快速Rhinomocks帮助

时间:2008-10-12 07:21:23

标签: unit-testing tdd rhino-mocks

有人可以看看这段代码并告诉我是否有任何明显的原因它不应该起作用?当在我的代码中调用service.getResponse时,模拟框架只返回null,而不是我指定的对象。

    [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();

    }

1 个答案:

答案 0 :(得分:0)

如果输入数据与预期值不匹配,动态模拟将返回null,因此我的猜测是您的代码调用具有RestRequestObject的不同值的service.GetResponse()或者RestRequestObject的相等性不起作用你期待它。

我想我会尝试用严格的模拟替换动态模拟,看看Rhino Mocks返回的错误。