'How to set up a stub and assigning a webresponse with Rhino mocking

I'm completely new to Rhino Mocks and mocking in general. I'm still wrapping my head around how to code the mocks.

I have a test class which contains something similar to this:

  [Test Class]     

    IGeneralMethods myGeneralMethods = new services.Models.GeneralMethods();
        MockRepository mockEngine = new MockRepository();
        IGeneralMethods simulatedService = mockEngine.DynamicMock<IGeneralMethods>();

        simulatedService.Stub(x => x.GetWebResponse().GetResponseStream() = myFakeResponse);

Now the last line (which doesn't work) I'm trying to create a stub and this is where I'm getting stuck. The bit of code that I want to stub is this:

public WebResponse GetWebResponse(WebRequest request)
    {
        try
        {
            WebResponse getResponse = request.GetResponse(); 
            return getResponse;
        }
        catch
        {
            //other actions
        }
        return null;
    }

I've another method where I can create a WebResponse so what I want to do is stop the GetWebResponse method from actually trying to make a request and just return my fake/prefabricated web response.

Where can I start?

Update

I've been tweaking with the code and sort of making headway. This is my latest code (work in progress):

        MockRepository mockEngine = new MockRepository();

        byte[] responseData = Encoding.UTF8.GetBytes("my XML here");
        Stream stream = new MemoryStream(responseData);
        WebRequest request = (WebRequest)mockEngine.StrictMock(typeof(WebRequest));
        WebResponse response = (WebResponse)mockEngine.StrictMock(typeof(WebResponse));
        Expect.On(request).Call(request.GetResponse()).Return(response);
        Expect.On(response).Call(response.GetResponseStream()).Return(stream);
        
       
        var stubbedGetWebResponse = mockEngine.Stub<IGeneralMethods>();

        stubbedGetWebResponse.Stub(x => x.ModeltoXml(newTicket)).Return("test");
        stubbedGetWebResponse.Stub(x => x.GetWebResponse(request)).Return(response);


        var test = new MyApi().NewIn(newTicket, referrer, stubbedGetWebResponse);

        stubbedGetWebResponse.VerifyAllExpectations();

I'm sure I'm not grasping something here as when I debug the test my ModeltoXml method doesn't return the string 'test'. I've stubbed this method because I also noticed that in my class under test which passes a populated model to it, the ModeltoXml method just returns null. So I'm guessing that Rhino has stubbed out all methods in my IGeneralMethods class.



Solution 1:[1]

Since WebRequest is an abstract class, try using a PartialMock. PartialMock will let you override those virtual methods.

// Arrange
var mockResp = MockRepository.GeneratePartialMock<WebResponse>();
var responseData = Encoding.UTF8.GetBytes("my XML here");
var stream = new MemoryStream(responseData);
mockResp.Expect(x => x.GetResponseStream()).Return(stream);

var mockReq = MockRepository.GeneratePartialMock<WebRequest>();
mockReq.Expect(x => x.GetResponse()).Return(mockResp);

// Act
// test here!

// Assert
mockReq.VerifyAllExpectations();
mockResp.VerifyAllExpectations();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adam Prescott