rehansaeed.github.io icon indicating copy to clipboard operation
rehansaeed.github.io copied to clipboard

[Comment] ASP.NET Core Integration Testing & Mocking using Moq

Open RehanSaeed opened this issue 5 years ago • 10 comments

https://rehansaeed.com/asp-net-core-integration-testing-mocking-using-moq/

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Leszek Leszek commented on 2020-01-03 13:48:39

Thank you very much, sir.

Another good article is on https://automationrhapsody.com/net-core-integration-testing-mock-dependencies/

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Rutxifer Rutxifer commented on 2020-01-03 18:04:48

Great article! Could be improved using HttpClient rather than HttpClientFactory? How it works if web application have a lot of dependencies? All of them are stored in properties when tests starts.

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2020-01-04 09:54:37

Great article! Could be improved using HttpClient rather than HttpClientFactory? How it works if web application have a lot of dependencies? All of them are stored in properties when tests starts.

Not sure what you mean. We're not using HttpClientFactory.

The only mocks you should have is for external systems like DB's.

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Nicole Nicole commented on 2020-03-30 03:48:37

Hi,

Thank you for the article.

However, I came across an issue when i tried this method.

this.ApplicationOptions = serviceProvider.GetRequiredService<IOptions>().Value;

The thing inside IOptions should be non-abstract, but ApplicationOptions is an abstract class.

Is there any possible way to fix that?

Cheers, Nicole

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Milind Milind commented on 2020-04-07 07:05:32

Getting error in CustomWebApplicationFactory.cs for this.Services.CreateScope() in ConfigureClient method. It does not recognize it. What is this.Services?

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2020-04-08 14:31:55

Hi,

Thank you for the article.

However, I came across an issue when i tried this method.

this.ApplicationOptions = serviceProvider.GetRequiredService<IOptions>().Value;

The thing inside IOptions should be non-abstract, but ApplicationOptions is an abstract class.

Is there any possible way to fix that?

Cheers, Nicole

ApplicationOptions is just a class that I myself wrote to contain some custom configuration options. It doesn't have to be abstract.

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2020-04-08 14:37:03

Getting error in CustomWebApplicationFactory.cs for this.Services.CreateScope() in ConfigureClient method. It does not recognize it. What is this.Services?

The class inherits from WebApplicationFactory and that is where the Services property comes from.

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Neal Neal commented on 2020-04-20 20:28:28

You don' t need to extend startup to configure your test services for dotnet core 3+. IWebHostBuilder has a ConfigureTestServices extension method that will allow you to update the service registrations. In your custom web application factory override ConfigureWebHost and call the ConfigureTestServices method from there. This is documented here.

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2020-04-21 09:33:56

You don' t need to extend startup to configure your test services for dotnet core 3+. IWebHostBuilder has a ConfigureTestServices extension method that will allow you to update the service registrations. In your custom web application factory override ConfigureWebHost and call the ConfigureTestServices method from there. This is documented here.

This is true. However, there was/is a known bug that stops this working at the time of writing. Also, I find that splitting it out into a separate class is helpful.

RehanSaeed avatar May 12 '20 09:05 RehanSaeed

Jonathan Jonathan commented on 2020-04-21 16:47:30

Nice article.

RehanSaeed avatar May 12 '20 09:05 RehanSaeed