【发布时间】:2011-11-23 19:45:03
【问题描述】:
我选择了这个解决方案:
Stubbing a property twice with rhino mocks
但即使我将两个存根都更改为 .Expect,第一个 Expect 也胜出:
这是单声道的娱乐:
using System;
使用 NUnit.Framework; 使用 Rhino.Mocks;
命名空间 FirstMonoClassLibrary { [测试夹具] 公共类TestingRhinoMocks { Sut _systemUnderTest; IFoo _dependency;
[SetUp]
public void Setup()
{
_dependency = MockRepository.GenerateMock<IFoo>();
_dependency.Expect(x => x.GetValue()).Return(1);
_systemUnderTest = new Sut(_dependency);
}
[Test]
public void Test()
{
_dependency.Stub(x => x.GetValue()).Return(2);
var value = _systemUnderTest.GetValueFromDependency();
Assert.AreEqual(2, value); // Fails says it's 1
}
}
public interface IFoo
{
int GetValue();
}
public class Sut
{
private readonly IFoo _foo;
public Sut(IFoo foo)
{
_foo = foo;
}
public int GetValueFromDependency()
{
return _foo.GetValue();
}
}
}
【问题讨论】:
-
一种选择是创建另一个具有不同设置的TestFixture;另一种方法是为 SUT 创建参数化工厂方法;第三个是避免在设置中设定期望。我过去遇到过类似的问题并切换到Moq。 :)
-
你怎么知道你的 test/sut 没有错? `_dependency.GetValue` 给你什么?测试它以查看您的模拟是否正常运行。
-
这里在 Mono 中重新创建它(目前只有我的 mac),但它仍然失败。想知道在我发布的另一个线程中为什么当它不起作用时被视为一种解决方案。
标签: c# unit-testing rhino-mocks