该单元测试如何转化为利用模拟框架?

时间:2012-10-03 03:05:50

标签: c# mocking tdd moq rhino-mocks

我正在尝试以TDD方式练习单元测试,所以我创建了一组接口,我认为可以在某种程度上独立地进行测试。我不确定我是否成功但我现在已经到了我非常确定是时候进行模拟框架了。我从例子中学得最好,我认为一个非常精通的人可能会很快将其转化为其中一个(Moq或Rhino Mocks也许?那些是我发现的那些)。

单元测试:

    [TestMethod]
    public void RendererRendersEachWidgetByCallingBeginRenderThenEndRender()
    {
        // Mocks/Stubs
        ISpace space = new MockSpace();
        IWorld world = new MockWorld();
        IWidget widget1 = new MockWidget("widget1");
        IWidget widget2 = new MockWidget("widget2");
        world.Add(widget1);
        world.Add(widget2);

        // Object under test
        IRenderer target = new Renderer(world, space);
        target.Render();

        // verify BeginRender is called
        Assert.IsTrue((space as MockSpace).results.Contains("Begin Render " + widget1.Id));
        Assert.IsTrue((space as MockSpace).results.Contains("Begin Render " + widget2.Id));

        // verify EndRender is called
        Assert.IsTrue((space as MockSpace).results.Contains("End Render " + widget1.Id));
        Assert.IsTrue((space as MockSpace).results.Contains("End Render " + widget2.Id));

        // verify BeginRender is called before EndRender
        var orderedResults = (space as MockSpace).results.Select((s, i) => Tuple.Create(i, s));
        Assert.IsTrue(
            (orderedResults.Single(x => x.Item2 == "Begin Render " + widget1.Id).Item1) <
            (orderedResults.Single(x => x.Item2 == "End Render " + widget1.Id).Item1));
        Assert.IsTrue(
            (orderedResults.Single(x => x.Item2 == "Begin Render " + widget2.Id).Item1) <
            (orderedResults.Single(x => x.Item2 == "End Render " + widget2.Id).Item1));
    }

1 个答案:

答案 0 :(得分:1)

这是一个Rhino.Mocks版本。我已经采取了自由Renderer的内部未知的地方。

正如您所看到的,涉及到相当多的设置,但在这种情况下,您不必创建所有这些设置 手动模拟对象类型。

[TestMethod]
public void RendererRendersEachWidgetByCallingBeginRenderThenEndRender()
{
    // Mocks/Stubs
      var orderedResultList = new List<string>();

    var space = MockRepository.GenerateStub<ISpace>();
      space.Stub(s => s.BeginRender(Arg<IWidget>.Is.Anything))
            .WhenCalled(x => orderedResultList.Add("Begin Render " + ((IWidget)x.Arguments[0]).Id));
      space.Stub(s => s.EndRender(Arg<IWidget>.Is.Anything))
            .WhenCalled(x => orderedResultList.Add("End Render " + ((IWidget)x.Arguments[0]).Id));

    var world = MockRepository.GenerateStub<IWorld>();

    var widget1 = MockRepository.GenerateStub<IWidget>();
      widget1.Stub(w => w.Id).Return("widget1"); // assuming no public setter for this property

    var widget2 = MockRepository.GenerateStub<IWidget>();
      widget2.Stub(w => w.Id).Return("widget2"); // assuming no public setter for this property

    var widgetList = new List<IWidget>{ widget1, widget2 };
      world.Stub(w => w.Widgets).Return(widgetList);

    // Object under test
    IRenderer target = new Renderer(world, space);
    target.Render();

    // verify BeginRender is called
      space.AssertWasCalled(s => s.BeginRender(Arg<IWidget>.Is.Same(widget1)));
      space.AssertWasCalled(s => s.BeginRender(Arg<IWidget>.Is.Same(widget2)));

    // verify EndRender is called
      space.AssertWasCalled(s => s.EndRender(Arg<IWidget>.Is.Same(widget1)));
      space.AssertWasCalled(s => s.EndRender(Arg<IWidget>.Is.Same(widget2)));

    // verify BeginRender is called before EndRender
    var orderedResults = orderedResultList.Select((s, i) => Tuple.Create(i, s));
    Assert.IsTrue(
        (orderedResults.Single(x => x.Item2 == "Begin Render " + widget1.Id).Item1) <
        (orderedResults.Single(x => x.Item2 == "End Render " + widget1.Id).Item1));
    Assert.IsTrue(
        (orderedResults.Single(x => x.Item2 == "Begin Render " + widget2.Id).Item1) <
        (orderedResults.Single(x => x.Item2 == "End Render " + widget2.Id).Item1));
}