0

Scenario: Validate that my class issues a RestRequest with the provided IRestclient where certain Parameters are set on the request.

    public class MyClass {
    private readonly IRestClient _client;

    public MyClass(IRestClient client) {
        _client = client;
    }

    void DoIt() {
        var req = new RestRequest { Method = WebMethod.Get, Path = "/DoIt" };
        req.AddParameter("key1", "value1");
        req.AddParameter("key2", "value2");

        var resp = _client.Request(req);
    }

}

[TestFixture]
public class MyClassTests {
    [Test]
    public void VerifyDoIt() {
        var client = MockRepository.GenerateStrictMock<IRestClient>();
        var resp = new RestResponse { StatusCode = HttpStatusCode.OK };
        client.Expect(x => x.Request(null)).IgnoreArguments().WhenCalled(inv => {
            var req = inv.Arguments[0] as RestRequest;
            //NO WAY TO VALIDATE THAT PARAMETERS HAS BEEN SET!
            inv.ReturnValue = resp;
        });
    }
}

Is Hammock simply not testable in this fashion or am I missing something crucial?

Edit: this is not a question about on how to access/validate method parameters with Rhino.Mocks, but rather how/if Hammock supports testing/validating that request parameters are set in unit-test scenarios.

Simon Söderman
  • 335
  • 1
  • 7
  • So do you want to check the values of the "req" variable created inside the DoIt() method? – PatrickSteele Nov 03 '11 at 11:59
  • sorry, I realize now that my question is a bit unclear to what solution I want. this is more of a Hammock question than a Rhino.Mocks question. eg. does hammock not support this, or is there some hidden feature in Hammock that lets one access and validate these things. – Simon Söderman Nov 03 '11 at 12:12

1 Answers1

0

If you need to validate the parameters sent to a mocked/stubbed method, you can use Rhino.Mocks' "GetArgumentsForCallsMadeOn" method. Pass it a lambda representing the call that was made and it will return you a jagged array. The first element ([0]) will be an array of arguments passed the first time the method was called. The second element ([1]) will be an array of arguments passed the second time the method was called, etc...

You could re-write your test like this to validate the request data:

var client = MockRepository.GenerateStub<IRestClient>();

var mc = new MyClass(client);
mc.DoIt();

client.AssertWasCalled(c => c.Request(null), o => o.IgnoreArguments());
var req = (RestRequest)client.GetArgumentsForCallsMadeOn(c => c.Request(null), o => o.IgnoreArguments())[0][0];

Assert.AreEqual("/DoIt", req.Path);
Assert.AreEqual(WebMethod.Get, req.Method);
PatrickSteele
  • 14,489
  • 2
  • 51
  • 54