Rhino AutoMocker和Stubs

时间:2010-02-19 15:06:37

标签: rhino-mocks

我使用Joshua Flanagan的文章“Auto mocking Explained”作为指南。在文章中有一节名为“相同的测试,automocker看起来像这样”。我使用这些信息来构建运行automocker的代码 正如您在下面看到的那样,答案是从BLL返回的列表。答案确实有一行;但是,所有字段都为空。因此对boo的测试失败了。任何提示和提示将不胜感激。

[Test]
        public void GetStaffListAndRolesByTeam_CallBLLWithDALStub()
        {
            // Build List<> data for stub
            List<StaffRoleByTeamCV> stubData = new List<StaffRoleByTeamCV>();
            StaffRoleByTeamCV stubRow = new StaffRoleByTeamCV();
            stubRow.Role = "boo";
            stubRow.StaffId = 12;
            stubRow.StaffName = "Way Cool";
            stubData.Add(stubRow);

            // create the automocker
            var autoMocker = new RhinoAutoMocker<PeteTestBLL>();

            // get instance of test class (the BLL)
            var peteTestBllHdl = autoMocker.ClassUnderTest;

            // stub out call to DAL inside of BLL
            autoMocker.Get<IPeteTestDAL>().Stub(c => c.GetStaffListAndRolesByTeam("4146")).Return(stubData);

            // make call to BLL this should return stubData
            List<StaffRoleByTeamCV> answer = peteTestBllHdl.GetStaffListAndRolesByTeam("4146");

            // do simple asserts to test stubData present
            // this passes
            Assert.IsTrue(1 == answer.Count,  "Did not find any rows"); 
            // this fails
            Assert.IsTrue(answer[0].Role == "boo", "boo was not found");  
        }

我尝试使用MockMode.AAA但仍然没有快乐

3 个答案:

答案 0 :(得分:1)

可以使用新版本的AutoMocker(1.0.3)。新版本支持中继模式,如本例所示。

[TestMethod]
    public void ShouldSupportOrderedTest()
    {
        //Arrange
        var autoMocker = new RhinoAutoMocker<CustomerUpdater>();

        var mockRepository = autoMocker.Repository;

        using (mockRepository.Ordered())
        {
            autoMocker.Get<ICustomerDataProvider>().Expect(x => x.GetCustomer(Arg<int>.Is.Anything)).Return(new CustomerItem());
            autoMocker.Get<ICustomerDataProvider>().Expect(x => x.UpdateCustomer(Arg<CustomerItem>.Is.Anything));
            autoMocker.Get<ILogWriter>().Expect(x => x.Write(Arg<string>.Is.Anything));
            autoMocker.Get<ILogWriter>().Expect(x => x.Write(Arg<string>.Is.Anything));
            autoMocker.Get<IMailSender>().Expect(x => x.SendMail(Arg<string>.Is.Anything, Arg<string>.Is.Anything));
        }

        //Act
        autoMocker.ClassUnderTest.UpdateCustomerName(1, "Frank", "frank@somecompany.com");

        //Assert
       ExceptionAssert.Throws<ExpectationViolationException>(mockRepository.VerifyAll,"IMailSender.SendMail(anything, anything); Expected #1, Actual #0.\r\nILogWriter.Write(anything); Expected #1, Actual #0.\r\n");  

    }

答案 1 :(得分:0)

我还没试过,但是本文建议默认情况下不会重放由automocker创建的所有模拟: http://www.lostechies.com/blogs/joshuaflanagan/archive/2008/09/25/arrange-act-assert-with-structuremap-rhinoautomocker.aspx

答案 2 :(得分:0)

是的,以前的版本都是如此。但改为支持版本1.0.3中的有序测试。