0

我正在尝试以 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));
    }
4

1 回答 1

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));
}
于 2012-10-03T03:45:07.473 回答