views:

90

answers:

1

Can someone take a look at this code and tell me if there's any obvious reason it shouldn't be working? When service.getResponse is called within my code the mocking framework only returns null, not the object I specified.

    [Test]
    public void Get_All_Milestones()
    {
        var mockRepo = new MockRepository();
        var service = mockRepo.DynamicMock<IRestfulService>();
        var request = new RestRequestObject
        {
            Password = "testpw!",
            UserName = "user",
            SecureMode = true,
            Url = "www.updatelog.com/",
            Command = String.Format("projects/{0}/milestones/list", 123456),
            Method = "POST"
        };
        var response = new RestResponseObject
                           {
                               StatusCode = 200,
                               ErrorsExist = false,
                               Response =
                                   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<milestones type=\"array\">\n  <milestone>\n    <completed type=\"boolean\">false</completed>\n    <created-on type=\"datetime\">2008-10-02T17:37:51Z</created-on>\n    <creator-id type=\"integer\">3028235</creator-id>\n    <deadline type=\"date\">2008-10-20</deadline>\n    <id type=\"integer\">7553836</id>\n    <project-id type=\"integer\">123456</project-id>\n    <responsible-party-id type=\"integer\">3028295</responsible-party-id>\n    <responsible-party-type>Person</responsible-party-type>\n    <title>Atb2 Editor Substantially Done</title>\n    <wants-notification type=\"boolean\">true</wants-notification>\n  </milestone>\n</milestones>\n"
                           };
        using(mockRepo.Record())
        {
            Expect
                .Call(service.GetResponse(request))
                .Return(response);
        }
        using(mockRepo.Playback())
        {
            var dal = new DataAccess(service);
            var result = dal.GetMilestones(123456);

            Assert.IsNotNull(result, "The result should not be null.");
            Assert.AreNotEqual(0, result.Count, "There should be exactly one item in this list.");
            Assert.AreEqual(123456, result[0].ProjectId, "The project ids don't match.");
            Assert.AreEqual(7553836, result[0].Id, "The ids don't match.");
        }

        mockRepo.VerifyAll();

    }
A: 

A dynamic mock will return null if the input data does not match the expected, so my guess would be that your code is calling service.GetResponse() with different values for the RestRequestObject or that equality for the RestRequestObject does not work as you expect it to.

I think I would try replacing the dynamic mock with a strict mock and look at the error Rhino Mocks returns.

Rasmus Faber