Bil*_*ell 11 c# unit-testing rhino-mocks arrange-act-assert
我一直在寻找有关使用Rhino Mocks 3.5+和AAA语法的一些不错的信息.我发现很多博客都混合了旧的和新的东西,似乎更难以弄清楚如何使用它.
如果有一个像早期版本那样的Rhino Mocks AAA Cheat Sheet,那将会很棒.您是否需要了解有关旧版Rhino的所有内容才能真正使用新版本?我敢肯定,如果我是专家,我会喜欢Rhino的所有功能,但是现在我只是在游泳信息.任何指针或良好的链接将完全赞赏!
我假设您熟悉官方文档,我认为这非常好。我的建议是尝试使用Rhino,当你遇到一些更具体的问题时,在SO或其他地方寻找解决方案。我认为没有针对 Rhino 模拟的全面备忘单。我想你会更幸运地问“我怎样才能使用 Rhino Mocks 做到这一点和那个”
编辑:嗯,当瞄准 AAA 时,您不需要使用录制/播放。AAA 涉及三个步骤:
。
IOmicronDll mockWrapper = MockRepository.GenerateMock<IOmicronDll>();
mockWrapper.Expect(wrapper => wrapper.Lock(1, ref errors)).OutRef(string.Empty).Return(true).Repeat.Any();
mockWrapper.Expect(wrapper => wrapper.Exec(1, "sys:cfg?(type)", ref output, ref errors)).OutRef("1,CMC 56,0;", "").Return(true).Repeat.Any();
mockWrapper.Expect(wrapper => wrapper.Exec("1", "sys:cfg?(type)", ref output, ref errors)).OutRef("1,CMC 56,0;", "").Return(true).Repeat.Any();
Microsoft.Practices.Unity.UnityContainer c = new Microsoft.Practices.Unity.UnityContainer();
c.RegisterInstance<IOmicronDll>(mockWrapper);
Run Code Online (Sandbox Code Playgroud)
Act,代表执行测试
public Omicron(int deviceID)
{
try
{
if (g_Omicron == null)
g_Omicron = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<CMEngineWrapper.IOmicronDll>();
m_UniqueIdentifier = Guid.NewGuid();
m_Logger = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<AdvAdmittance.Framework.ILogger>();
m_ID = deviceID;
GetConfiguration();
g_InstancesCount++;
m_PollThread = new Thread(new ThreadStart(DoPoll));
m_PollThread.Start();
}
Run Code Online (Sandbox Code Playgroud)和Assert,它代表veryfing the results
。
Assert.AreEqual("CMC 56", omicron.Type);
mockWrapper.AssertWasCalled(wrapper => wrapper.Release(), options => options.Repeat.AtLeastOnce());
Run Code Online (Sandbox Code Playgroud)
也许上面的例子不是最好的,但可能会让你找到正确的方向。
归档时间: |
|
查看次数: |
5486 次 |
最近记录: |