Rhino Mocks:AAA Synax:Assert 属性设置为给定类型 [英] Rhino Mocks: AAA Synax: Assert property was set with a given type

查看:55
本文介绍了Rhino Mocks:AAA Synax:Assert 属性设置为给定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图断言模拟对象中的属性是使用给定类型设置的.该属性具有抽象类型,并使用多种具体类型之一进行设置.

I am trying to assert that a property in a mock object was set with a given type. The property has an abstract type and is set with one of a number of concrete types.

这就是我想要做的,无论 Foo.DoSomething() 设置 Foo.Bar 的值是什么,它总是通过测试:

This is what I'm trying to do, and it's always passing the test regardless of the value that Foo.DoSomething() sets Foo.Bar with:

    [Test]
    public void DoSomething_SetsCorrectBar()
    {
        // Arrange
        Foo foo = MockRepository.GenerateMock<Foo>(); // Creates mock in Replay mode (what I want for AAA syntax).

        // Act
        foo.DoSomething();

        // Assert that DoSomething set Foo.Bar to an instance of CorrectBarSubclass
        foo.AssertWasCalled(foo => foo.Bar = null, options => options.WhenCalled(invocation => Assert.That(invocation.Arguments[0] is CorrectBarSubclass)));
    }

Rhino 3.5/AAA 文档 描述了如何设置对具有给定值的属性集的期望,但我只想检查该值的类型.

The Rhino 3.5 / AAA Documentation describes how to set expectations on property sets having a given value, but I just want to check the type of the value.

如何对属性集进行断言,特别是对具有给定参数类型的属性集?

How does one assert on a property set, specifically on a property set having a given parameter type?

更新:上面的例子过于简单了.我实际测试的是一个单独的状态类.它是父对象"(在本例中为具有状态的对象)可以处于的几种状态之一.我正在验证被测状态(称为 BarOne)是否正确地将 Foo.State 设置为 BarTwo 的一个实例什么时候该转换状态了.

Update: The example above is oversimplified. What I'm actually testing is an individual state class. It's one of several states that a "parent object" (the object having the state, Foo in this case) can be in. I was verifying that the state under test (call it BarOne) correctly set Foo.State to an instance of BarTwo when it was time to transition states.

一个更清晰的例子(实施了公认的解决方案)是:

A clearer example (with the accepted solution implemented) would be:

    [Test]
    public void BarOne_DoSomething_SetsNextState()
    {
        // Arrange
        Foo foo = MockRepository.GenerateMock<Foo>(); // Creates mock in Replay mode (what I want for AAA syntax).
        foo.Stub(x => x.CreateBarTwoState()).Return(new BarTwo(foo));
        BarOne bar = new BarOne(foo); // We are testing the BarOne state independently of Foo, that's why we mock Foo but instantiate BarOne.

        // Act
        bar.DoSomething();

        // Assert that DoSomething set Foo.Bar to an instance of BarTwo
        foo.AssertWasCalled(foo => foo.Bar = Arg<BarTwo>.Is.TypeOf);
    }

推荐答案

也许是这样的:

[Test]
public void AddPlayer_GivesGameEnoughPlayersToStart_SetsNextState()
{
    // Arrange
    Foo foo = MockRepository.GenerateMock<Foo>(); // Creates mock in Replay mode (what I want for AAA syntax).
    foo.Expect(m => m.Bar = Arg<CorrectBarSubclass>.Is.TypeOf);
    // Act
    foo.DoSomething();
    //Assert
    foo.VerifyAllExpectations();
}

这是怎么回事..

我们将断言更改为 Expect.我觉得这更简洁一些,而且期望允许我们更清晰地验证类型是什么.我们说期望 Bar 将被设置为 CorrectBarSubclass 的一个实例.然后我们采取行动,并断言我们的期望得到了满足.

We changed the assertion to be an Expect. I find this a little bit cleaner, also the expectation allows us a cleaner verification of what the type is. We are Saying "Expect that Bar will be set to an instance of CorrectBarSubclass. Then we Act, and we assert that our expectation was met.

几件事:任何时候你 Mock 一个类,任何你有 ExpectStub 调用的东西都必须是虚拟的或抽象的,所以在这种情况下,Bar 必须是虚拟的.模拟接口并测试类如何使用依赖项通常总是更好,而不是测试类如何使用自身(这通常表明过度测试或不正确的关注点分离).

Couple of things: Any time you Mock a class, anything you have an Expect or Stub call on must be virtual or abstract, so in this case, Bar must be virtual. It's usually always better to mock an interface and test how a class uses a dependency, rather than testing how a class uses itself (that usually is an indication of over-testing, or incorrect separation of concerns).

在您的情况下,甚至需要模拟吗?您只是使用一种有点复杂的语法来断言真实行为的结果,除了属性的 setter 之外,没有什么是真正被嘲笑的.有时,测试真实行为更容易、更合适.为什么不做这样的事情:

In your case, is a mock even required? You are just using a somewhat complicated syntax to assert the result of a real behavior, nothing is really mocked other than the setter of a property. Sometimes it's just easier, and more appropriate, to test real behaviors. Why not do something like this:

var foo = new Foo();
foo.DoSomething();
Assert.That(foo.Bar is CorrectBarSubclass);

这篇关于Rhino Mocks:AAA Synax:Assert 属性设置为给定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆