I'm completely new to Rhino Mocks ... actually mocking at all. I am still pondering how to code the ridicule, and would really appreciate some recommendations.
I have a test class that 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 that is where I get stuck. The bit of code I want to drown out is this:
public WebResponse GetWebResponse(WebRequest request)
{
try
{
WebResponse getResponse = request.GetResponse();
return getResponse;
}
catch
{
}
return null;
}
I have another method where I can create a WebResponse, so I want to stop the GetWebResponse method from trying to make a request and just return my fake / prefabricated web response.
However, I do not have Scooby, where to start?
UPDATE
I tuned the code and took a step forward. This is my last 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();
, - , ModeltoXml "test". , , , , ModeltoXml null. , Rhino IGeneralMethods.