views:

241

answers:

4

Hello,

I am trying to write a unit test for an action method which calls the Controller.RedirectToReferrer() method, but am getting a "No referrer available" message.

How can I isolate and mock this method?

Thanks, -- rauchy

+1  A: 

Have you thought about creating a test double?

Chris Canal
That might help, so will a mock, I assume.The problem is that the PrepareController() method used in Monorail controller unit tests already creates a stub, and the Response object is immutable and has no setter.Any other ideas? (Typemock isn't an option ATM)
Omer Rauchwerger
+1  A: 

This question was answered on the castle forums: http://forum.castleproject.org/viewtopic.php?p=13743

Mauricio Scheffer
A: 

In my version of the trunk I'm working against, r5299, I had to do this to mock out RedirectToReferrer. I think it's been changed in recent commits, I'm not sure.

[TestFixture]
public class LoginControllerTests : GenericBaseControllerTest<LoginController>
{
    private string referrer = "http://www.example.org";
    protected override IMockRequest BuildRequest()
    {
        var request = new StubRequest(Cookies);
        request.UrlReferrer = referrer;

        return request;
    }

    protected override IMockResponse BuildResponse(UrlInfo info)
    {
        var response = new StubResponse(info,
                                        new DefaultUrlBuilder(),
                                        new StubServerUtility(),
                                        new RouteMatch(),
                                        referrer);
        return response;
    }

etc. etc.

It's oddly the Response that you need to molest to get the RedirectToReferrer to work. I had to crawl around in the monorail sources to figure it out.

Cheers!

James Thigpen
A: 

That helped me a lot, very quickly, thanks!