7

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);
});
ullmark
  • 2,469
  • 1
  • 19
  • 28
  • This sounds like something to be avoided. Can you give more detail on why you want to do it? – JontyMC Mar 02 '11 at 15:29
  • 1
    I think the author of Deleporter explains why you'd want to do this pretty good in his blog post; http://blog.stevensanderson.com/2010/03/09/deleporter-cross-process-code-injection-for-aspnet/ Change the applications configuration to Test for an example... I wanted to test my entire application in my specs, not just the controllers.. – ullmark Mar 02 '11 at 16:54
  • I wanted to test my application from the outside in. Since it's a Rest API, the first thing to test are the HTTP calls... – ullmark Mar 02 '11 at 16:56

2 Answers2

1

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?

jammus
  • 2,540
  • 23
  • 28
  • I added a little more info about what I was trying to do in deleporter. I'm storing my entities in RavenDB and have now been informed of a better way of doing this kinds of test with raven. I'll just start a raven service inside my Specs project when the tests run... – ullmark Mar 03 '11 at 08:56
-1

I think you're probably looking for WatiN

Scott Arrington
  • 12,325
  • 3
  • 42
  • 54
  • Does WatiN allow for the kind of cross-process mocking that the OP describes? – Robert Harvey Feb 28 '11 at 17:09
  • WatiN basically opens a browser window for you and does a sort of integration test. It's not ideal for API testing, but it's about the only option I've seen out there. I know there was some talk a while back about creating a Webrat for .NET, but I'm not sure anyone has run with that. – Scott Arrington Feb 28 '11 at 17:14
  • I was under the impression that WatiN accually starts the browser and does whatever you tell it in code. Not being able to inject stuff in the web app at all? – ullmark Feb 28 '11 at 17:31
  • Since doing the HTTP call isn't really a problem (using .NETs WebClient class for an example) It's the cross process injection I need to achieve... – ullmark Feb 28 '11 at 17:33