vendredi 1 juillet 2016

Unit test for http web request and response in c#

I want to write a unit test for http web request and response method. Please find the below method,

 public string GetEmployeeId()
        {

                var tokenRequest = (HttpWebRequest)WebRequest.Create("http://www.goggle.com");
                tokenRequest.Method = "POST";
                tokenRequest.ContentType = "application/x-www-form-urlencoded";

                var bytes = Encoding.UTF8.GetBytes(GetKeys(credentials));
                tokenRequest.ContentLength = bytes.Length;

                Response response;
                try
                {
                    using (var stream = tokenRequest.GetRequestStream())
                    {
                        stream.Write(bytes, 0, bytes.Length);
                        stream.Flush();

                        using (var webResponse = request.GetResponse())
                        {
                            Stream receiveStream = webResponse.GetResponseStream();
                            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
                            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(readStream.ReadToEnd()));
                            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
                            response = ser.ReadObject(ms) as Response;
                            ms.Close();
                            readStream.Close();
                        }
                    }
                }
          return response.Id;

        }

  private string GetKeys(Credentials credentials)
        {
            return String.Format(@"client_id={0}&client_secret={1}&grant_type=client_credentials",
                                credentials.Id, credentials.Secret);
        }

I dont know how to write unit test for web request methods.Can anyone suggest how to write unit test for the above method?

Expected method instancemethod.__call__ not called

I am trying to modify an old test case using new code. I am getting an error

`mox3.mox.ExpectedMethodCallsError: Verify: Expected methods never called:
 0.  instancemethod.__call__(<IgnoreArg>, content=<IgnoreArg>, extra_md=  <IgnoreArg>, network_info=<IgnoreArg>) -> None

How do I know with this information that which method has not been called ? Also what has been called in its place. AFAIK the -> None would mean nothing has been called in its place I am open to using pdb or any other technique that would help.

TIA

`

What is Major Difference between Integration Testing and Behavioral Testing BDD

Over the last few years TDD get very popular and some home another version of testing comes up wit Integration testing and now Behavioral testing BDD is the major focus can some one explain the major difference between BDD and Integration testing.

How to Unit Test ASP.NET CORE controller with MSTEST and add Fakes

I have an MVC project in ASP.NET CORE. I have added a class library and added testing frameworks in project.json. The test methods seems to execute and run.. but i have scenarios where i need to use shims and stubs.. For example Web Client or a an interface method. I am unable to add fakes for dll's in ASP.NET CORE.. How do i do it? Is there any method to shim methods like Web client in core?

Dependencies added: "dependencies": { "NETStandard.Library": "1.6.0", "dotnet-test-mstest": "1.0.1-preview", "MSTest.TestFramework": "1.0.0-preview" },

Python - Click command line unit test case for input prompt from command line function

I am working on writing unit test cases for a command line application in python using click library.

I tried below example and this is working fine:

def test_hello_world():
    @click.command()
    @click.argument('name')
    def hello(name):
        click.echo('Hello %s!' % name)

    runner = CliRunner()
    result = runner.invoke(hello, ['Yash'])
    assert result.exit_code == 0
    assert result.output == 'Hello Yash!\n'

But now I want to input prompt from my function.

like this:

def test_name_prompt(self):
    @click.command()
    @click.option('-name', default=False)
    def username():
        fname = click.prompt("What's your first name?")
        lname = click.prompt("what's your last name?")
        click.echo("%s %s" % (fname, lname))

    runner = CliRunner()
    result = runner.invoke(username, ['-name'])
    assert result.exit_code == 0      
    assert result.output == 'Yash Lodha'

how to run spring-boot integration tests with gradle on multiple servers

i have some integration tests for my spring boot application. based on dependencies (and classpath jars) spring boot chooses a server to start: (tomcat is there is only spring-boot-starter-web, undertow if there is spring-boot-starter-undertow or jetty if there is spring-boot-starter-jetty)

i'm writing a filter that is supposed to work on many different servers. i don't have compile dependency on any server but i would like to test my code on many servers. how can i do it?

for sure one way is to make gradle script set dependencies based on some env variable and then just call gradle test a few times with different env variable values. is there any simpler way, so i can test everything at once? like starting the servers programmatically in tests? or using some gradle/spring plugin?

Can we write Automated Unit Test in Pega Tool?

We are using Pega Tool for Developing a business process. Wondering can we write an Automated unit Test cases in Pega Tool? I have also referred to Pega PDN but not really how this can be achieved.