Code Monkey home page Code Monkey logo

avanade / unittestex Goto Github PK

View Code? Open in Web Editor NEW
18.0 5.0 4.0 546 KB

UnitTestEx provides .NET testing extensions to the most popular testing frameworks (MSTest, NUnit and Xunit) specifically to improve the testing experience with ASP.NET controller, and Azure Function, execution including underlying HttpClientFactory mocking.

License: MIT License

PowerShell 0.63% C# 99.37%
testing unit-testing unit-test mstest xunit nunit api-testing function-testing azure-functions httpclientfactory httpclient integration-testing

unittestex's People

Contributors

adithakker avatar chullybun avatar israels avatar karpikpl avatar sealjay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

unittestex's Issues

Enhancement: Lambda Assertions

I have a function that takes an entity from an Azure Storage Queue and places it in an Azure Storage Table. I am using this library's TypeTester to test it. It works great, but I can only assert that the operation was successful.

Because there are fields in my table entity that are generated on-the-fly, I can only call AssertSuccess(). I can't use AssertValue(). My only recourse is to visually examine the log output and say, "Yep, that looks good."

It would be great if I could use a lambda with the ValueAssertor<TValue> and perform my comparisons only on the fields I want to check in my TValue.

Issue with Asserts?

I have a following tests that passes with "UnitTestEx.Xunit" Version="2.1.1"

		// Arrange
		using var test = CreateApiTester<Startup>();

		// Act
		var result = test
			.Controller<SampleController>()
			.Run(c => c.Get("thisIsATest"))
			.AssertOK()
			.AssertJson("{\"data\": \"this_is_a_test\"}");

but this also passes and shouldn't

		// Act
		var result = test
			.Controller<SampleController>()
			.Run(c => c.Get("thisIsATest"))
			.AssertOK()
			.Assert(new { foo="bar"});

UnitTests fail when there's a comment in local.settings.json

To reproduce add a comment to settings file

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
  // test comment
    }
}

and run a test with using var test = FunctionTester.Create<Startup>();

HttpTriggerTester Expression Tree Limitations

HttpTriggerTester Expression Tree Limitations

Background

In my tests project, I have created an extension method using reflection to automatically get the route of my HTTP-Triggered-Functions with that method.
The goal here was to eliminate requirements for test code updates after changes to a Function's route in the main code. It almost works.

I've since written my code around this limitation, but it requires me to spin up & run an entire HostTesterBase implementor twice. 😰

Issue

HttpTriggerTester's Run/RunAsync methods, unlike TypeTester's, uses expression trees. SRC

public async Task<ActionResultAssertor> RunAsync(Expression<Func<TFunction, Task<IActionResult>>> expression)

This means that I am unable to write something like this without experiencing compilation errors:

        var sut = await test
            .HttpTrigger<QueueTaskCreateFunction>()
            .RunAsync(f => f.Run(test.CreateHttpRequest(HttpMethod.Post, f.GetFunctionRoute(), contentType: MediaTypeNames.Text.Plain), context.Object));

A statement like this would work with TypeTester, but I can't use TypeTester to test my HTTP-Triggered-Function.

Is there a technical reason for HttpTriggerTester's Run/RunAsync methods not having an overload for Action<T> or Func<T1,T2>??

Workaround

The following boilerplate accomplishes what I intended without any issues:

       var route = test
            .Type<QueueTaskCreateFunction>()
            .Run(f => f.GetFunctionRoute())
            .Result;

        var request = test
            .CreateHttpRequest(HttpMethod.Post, route, contentType: MediaTypeNames.Text.Plain);

        var sut = await test
            .HttpTrigger<QueueTaskCreateFunction>()
            .RunAsync(f => f.Run(request, context.Object));

HTTP Functions that use Microsoft.Azure.Functions.Worker.Http

I have an HTTP-triggered Azure function with the following signature, using the Microsoft.Azure.Functions.Worker.Http.* classes for the request and response data.

[Function(...)]
public async Task<HttpResponseData> Run([HttpTrigger(...)] HttpRequestData req, FunctionContext context)
{
/*...*/
}

I was unable to get UnitTestEx to work with this setup and had to do my unit testing with Moq and mock classes for those request/response classes.

My functions project uses the isolated worker model, and I followed the example setup the best I could.
In the end, I was unable to use this library because it could not handle HttpRequestData/HttpResponseData.

I am still new to using Azure Functions, so if I can switch these over to what UnitTestEx supports, I'm unaware. 😃

Security Policy violation Outside Collaborators

This issue was automatically created by Allstar.

Security Policy Violation
Found 1 outside collaborators with admin access.
This policy requires users with this access to be members of the organisation. That way you can easily audit who has access to your repo, and if an account is compromised it can quickly be denied access to organization resources. To fix this you should either remove the user from repository-based access, or add them to the organization.

OR

If you don't see the Settings tab you probably don't have administrative access. Reach out to the administrators of the organisation to fix this issue.

OR

  • Exempt the user by adding an exemption to your organization-level Outside Collaborators configuration file.

This issue will auto resolve when the policy is in compliance.

Issue created by Allstar. See https://github.com/ossf/allstar/ for more information. For questions specific to the repository, please contact the owner or maintainer.

Result not being output in log correctly

For example, the TypeTester logs the result. As the result maybe IActionResult, this has no inherent properties so when serialized using default serializer an empty object is returned {}. To correctly enable for logging the type should be specified as dynamic:

Implementor.WriteLine(JsonSerializer.Serialize<dynamic>(result, JsonWriteFormat.Indented));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.