1

I'm using MvcContrib's test helpers and Rhino Mocks 3.5 to test an ASP.NET MVC action method. I build my fake controller like so:

var _builder = new TestControllerBuilder();
_builder.InitializeController(_controller);

So I get a fake controller that contains fake HTTP Server etc.

I'm then trying to stub the Server.MapPath method like so

controller.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("/APP_DATA/Files/");

but in my method under test the call to Server.MapPath("/APP_DATA/Files/") returns null.

This is the test

    const string STOCK_NUMBER_ID = "1";
    const string FULL_FILE_PATH = "App-Data/Files";

    var controller = CreateStockController();
    _uploadedFileTransformer.Stub(x => x.ImageBytes).Return(new byte[10]);
    _uploadedFileTransformer.Stub(x => x.ConvertFileToBytes(FULL_FILE_PATH)).Return(true);

    controller.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("/App_Data/Files/");

    controller.AddImage(Guid.NewGuid(), STOCK_NUMBER_ID);

What I am missing?

Simon Lomax
  • 8,714
  • 8
  • 42
  • 75
  • Could we see the test? Looks like a possible missing repository.Playback(). – PatrickSteele Jul 14 '10 at 16:27
  • I've added the test to the OP. I didn't think we needed to use record and playback in Rhino Mocks v3.5 - am I wrong? – Simon Lomax Jul 14 '10 at 16:33
  • Could you also paste `CreateStockController()`? I am not familiar with test helper you are using, but where is controller.Server created? – Grzenio Jul 14 '10 at 16:39
  • Good point, in fact after further investigation I find its not being created. How would I create it and have appear as part of my fake controller? Is it possible? I'm wondering why the MvcContrib Testhelpers don't already do it. – Simon Lomax Jul 14 '10 at 20:07
  • Record/Playback is not REQUIRED, but some people still use it. Frequently, they do a record(), but forget to execute the playback() and their stubs/mocks don't work. However, I just did a test on a sample MVC project using MVCContrib and I'm seeing the same thing -- the controller.Server is a Rhino.Mocks proxy, but the MapPath stub isn't working. I'm going to play around and see what is happening. – PatrickSteele Jul 15 '10 at 13:11

2 Answers2

1

Old post but I was searching for this and I found a solution, MvcContrib's TestHelper probably got it fixed because for me it's working.

_builder.HttpContext.Server.Stub(s => s.MapPath("~/" + filepath)).Repeat.Once().Return(mapedPath);
VinnyG
  • 6,883
  • 7
  • 58
  • 76
0

Looks like this is a bug in MVCContrib (at least with what I have on my machine -- v1.0.0.0). When setting up the controller context, it's using Rhino.Mocks record/replay mode, but (and this is the bug), it doesn't put the HttpServer mock into replay mode. It puts everything else in replay mode but not that one.

So a quick fix is to do:

controller.Server.Replay();

As part of your "arrange" section of your test. Then it works fine.

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
  • Patrick, I tried what you suggested, I added controller.Server.Replay() but I still have the same problem. I'm using MvcContrib v2.0.36.0 with MVC2. I seem to have ages on this problem so in the end I added a HTTPServerUtilityBase parameter to my action method and created a custom model binder which provides me with a Server object. Then in my test I created my own stub of HttpServerUtilityBase and stubbed out the MapPath method. Its all working nicely but I would have preferred that the TestHelper provide me with the correct stub in the first place. – Simon Lomax Jul 15 '10 at 15:45
  • Ok. The sample project I tried it on was MVC1. Looks like the bug still exists in MVC2. Should be easy to fork the code from CodePlex, fix and submit. Glad you got it working! – PatrickSteele Jul 15 '10 at 16:39