Skip to content

VerifyTests/Verify.Mockly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.Mockly

Discussions Build status NuGet Status

Adds Verify support for verifying Mockly types.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

Developed using JetBrains IDEs

JetBrains logo.

NuGet

Usage

[ModuleInitializer]
public static void Init() =>
    VerifyMockly.Initialize();

snippet source | anchor

An HttpMock and its captured requests can then be verified:

[Test]
public async Task VerifyGetRequest()
{
    var mock = new HttpMock();

    mock.ForGet()
        .ForHttps()
        .ForHost("api.example.com")
        .WithPath("/api/users/123")
        .RespondsWithJsonContent(new { id = 123, name = "John" });

    var client = mock.GetClient();
    await client.GetAsync("https://api.example.com/api/users/123");
    await Verify(mock);
}

snippet source | anchor

Results in:

[
  {
    Method: GET,
    Scheme: https,
    Host: api.example.com,
    Path: /api/users/123,
    WasExpected: true,
    StatusCode: OK
  }
]

snippet source | anchor

Verifying POST Requests with Body

[Test]
public async Task VerifyPostRequest()
{
    var mock = new HttpMock();

    mock.ForPost()
        .ForHttps()
        .ForHost("api.example.com")
        .WithPath("/api/users")
        .RespondsWithStatus(System.Net.HttpStatusCode.Created);

    var client = mock.GetClient();
    await client.PostAsync(
        "https://api.example.com/api/users",
        new StringContent(
            """{"name":"Jane"}""",
            Encoding.UTF8,
            "application/json"));
    await Verify(mock);
}

snippet source | anchor

Results in:

[
  {
    Method: POST,
    Scheme: https,
    Host: api.example.com,
    Path: /api/users,
    Body: {"name":"Jane"},
    WasExpected: true,
    StatusCode: Created
  }
]

snippet source | anchor

Verifying a RequestCollection

[Test]
public async Task VerifyRequestCollection()
{
    var mock = new HttpMock();
    var requests = new RequestCollection();

    mock.ForGet()
        .ForHttps()
        .ForHost("api.example.com")
        .WithPath("/api/users/*")
        .CollectingRequestsIn(requests)
        .RespondsWithJsonContent(new { id = 1, name = "John" });

    var client = mock.GetClient();
    await client.GetAsync("https://api.example.com/api/users/1");
    await client.GetAsync("https://api.example.com/api/users/2");
    await Verify(requests);
}

snippet source | anchor

Results in:

[
  {
    Method: GET,
    Scheme: https,
    Host: api.example.com,
    Path: /api/users/1,
    WasExpected: true,
    StatusCode: OK
  },
  {
    Method: GET,
    Scheme: https,
    Host: api.example.com,
    Path: /api/users/2,
    WasExpected: true,
    StatusCode: OK
  }
]

snippet source | anchor

Scrubbing Members

Members can be scrubbed by name:

[Test]
public async Task ScrubBody()
{
    var mock = new HttpMock();

    mock.ForPost()
        .ForHttps()
        .ForHost("api.example.com")
        .WithPath("/api/users")
        .RespondsWithStatus(System.Net.HttpStatusCode.Created);

    var client = mock.GetClient();
    await client.PostAsync(
        "https://api.example.com/api/users",
        new StringContent(
            """{"name":"Jane"}""",
            Encoding.UTF8,
            "application/json"));
    await Verify(mock)
        .ScrubMember("Body");
}

snippet source | anchor

Results in:

[
  {
    Method: POST,
    Scheme: https,
    Host: api.example.com,
    Path: /api/users,
    Body: Scrubbed,
    WasExpected: true,
    StatusCode: Created
  }
]

snippet source | anchor

About

Adds Verify support for verifying Mockly types.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors

Languages