Skip to content
fonlow edited this page Sep 7, 2016 · 17 revisions

This component set is for you to construct unit testing and integration testing with dependencies on external resources like services, databases and security etc.

The documentation here is minimal. And in the following chapters, if no detailed description is given, you are going to read the doc comments of classes and the cases of the unit testing or integration testing.

Assembly Fonlow.Testing.Service

NuGet: https://www.nuget.org/packages/Fonlow.Testing.Service

Class IisExpressAgent

Launch IIS Express.

Test case: IntegrationTests.WCFTests.TestGetData

Example config:

<add key="Testing_UseIisExpress" value="True" />
<add key="Testing_HostSite" value="Fonlow.DemoService" />
<add key="Testing_HostSiteApplicationPool" value="Clr4IntegratedAppPool" />
<add key="Testing_SlnRoot" value="C:\VSProjects\FonlowTesting" />
<add key="Testing_BaseUrl" value="http://localhost:4060/" />

Class IisExpressFixture

Mainly for launching IIS Express only once for one or multiple test classes in the same assembly that talk to the same Website.

Test cases: IntegrationTests.IisExpressFistureTests

Assembly Fonlow.Testing

NuGet: https://www.nuget.org/packages/Fonlow.Testing

Class PermissionPrincipalFixture

For functions of a service class decorated by SecurityPermissionAttribute.

Service function sample:

    [PrincipalPermission(SecurityAction.Demand, Role = RoleConstants.Admin)]
    public void DeleteAccountByUserName(string userName)
    {
        RemoveAccountByUserName(userName);
    }

Test case sample:

        var service2 = new UserManagementImp();
        Fonlow.Testing.PermissionPrincipalFixture.SetPrincipal("Admin");
        service2.DeleteAccountByUserName(loginName);

Class SetupAndTearDownFixture

A base class for creating fixtures that could setup and tear down. The derived class is to be used with XUnit.ICollectionFixture.

Assembly Fonlow.Testing.Http

NuGet: https://www.nuget.org/packages/Fonlow.Testing.Http

Class HttpClientWithUsername

Wrap a HttpClient instance with a bearer token initialized through username and password. A HttpClient instance could be used concurrently to send multiple requests. Having a bearer token will make subsequent request fast without the need to login again and again.

Class DefaultHttpClientFixture

If all test cases in an assembly are using the same base URL and the same credential, you may use the fixture along with XUnit.ICollectionFixture.

Sample:

public class TestConstants
{
    public const string IisExpressAndInit = "IISExpressStartup";
}

/// <summary>
/// Load IIS Express once for all test classes using the same Collection
/// http://xunit.github.io/docs/shared-context.html
/// </summary>
[CollectionDefinition(TestConstants.IisExpressAndInit)]
public class IisCollection : ICollectionFixture<Fonlow.Testing.IisExpressFixture>
{
    // This class has no code, and is never created. Its purpose is simply
    // to be the place to apply [CollectionDefinition] and all the
    // ICollectionFixture<> interfaces.
}

[Collection(TestConstants.IisExpressAndInit)]
public class ValuesTests : IClassFixture<DefaultHttpClientFixture>
{
    public ValuesTests(DefaultHttpClientFixture restfulFixture)
    {
        baseUri = restfulFixture.BaseUri;
        accessToken = restfulFixture.AccessToken;
        authorizedClient = restfulFixture.AuthorizedClient;
        api = new Values(authorizedClient, baseUri);
    }

    Uri baseUri;

    string accessToken;

    HttpClient authorizedClient;

    Values api;

    [Fact]
    public void TestValuesGet()
    {
        var array = api.Get().ToArray();
        Assert.Equal("value2", array[1]);
    }

    [Fact]
    public void TestValuesGetId()
    {
        var r= api.Get(1, "something to say");
        Assert.Equal("something to say1", r);
    }

    [Fact]
    public void TestValuesGetId2()
    {
        var task = authorizedClient.GetStringAsync(new Uri(baseUri, "api/Values?id=1&name=value"));
        var text = task.Result;
        var jObject = JValue.Parse(text);
        Assert.Equal("value1", jObject.ToObject<string>());
    }

    [Fact]
    public void TestValuesPost()
    {
        var t = api.Post("value");
        Assert.Equal("VALUE", t);
    }

    [Fact]
    public void TestValuesPut()
    {
        var t = authorizedClient.PutAsync(new Uri(baseUri, "api/Values/1"), new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("", "value") }));
        var ok = t.Result.IsSuccessStatusCode;
        Assert.True(ok);

    }
Clone this wiki locally