Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross Process Mocking in ASP.NET

I'm building a REST API using ASP.NET MVC 3. I'm doing it BDD-style using SpecFlow with NUnit as test runner.

Since it's a REST API, testing the Url:s are obviously very important so I want to be able to make real HTTP calls in the specs.

I'm now looking for tips on how to get Cross Process Mocking going. In short I want to mock the data layer with entities that I generate in the Specs.

In a Rails application I would use Webrat for this. Is there anything equivalent to that in .NET yet?

I've already tried Deleporter but it doesn't seem to be able to "send" advanced constructs (Creating a simple string in the specs and using it in Deleporter works, but doesn't for a custom class the properties all becomes null)

Does anyone have experiences or tips on how to do this?

Edit: What I was trying do to in Deleporter was something like this (I am aware that i could generate the models inside the Deleporter code but this is a simplified example, so that wouldn't work for me):

var models = Builder<Foo>.CreateListOfSize(300);
Deleporter.Run(() =>
{
  var mockService = new Mock<IFooService>();
  // Models will be a list of 300 Foos but the foos properties will all be null
  mockService.Setup(s => s.GetStuff()).Returns(models);
  ObjectFactory.Inject(mockService.Object);
});
like image 459
ullmark Avatar asked Feb 28 '11 16:02

ullmark


People also ask

What is WebApplicationFactory?

WebApplicationFactory<TEntryPoint> is used to create a TestServer for the integration tests. TEntryPoint is the entry point class of the SUT, usually Program.

What is mock test in .NET core?

A mock version of something is an object that can act like the real thing but can be controlled in test code. Moq (pronounced “mok u” or “mock”) is a library available on NuGet that allows mock objects to be created in test code and it also supports . NET Core.

Does MOQ support .NET core?

Moq is a library that allows us to create mock objects in test code. It is also available in NuGet. This library also supports . NET Core.

What is unit test in ASP net Core?

Unit testing involves testing a part of an application in isolation from its infrastructure and dependencies. When you unit test controller logic, only the content of a single action or method is tested, not the behavior of its dependencies or of the framework itself.


1 Answers

I'm just investigating this myself. Looking at some of the example code in the GuestBookDemo it seems possible to use Deleporter for this.

Do you have an example of exactly what you'd like to be able to do?

like image 125
jammus Avatar answered Sep 20 '22 17:09

jammus