StructureMap RhinoMock 记录/回放,需要示例
我正在寻找一些有关如何使用 StructureMap 或 Unity 与 NUnit 进行以下模拟测试的示例。
我有以下代码结构
public interface IDAL
{
List<Model> Method1(int id);
}
public class DAL : IDAL
{
public List<Model> Method1(int id)
{
List<Model> retval = new List<Model>();
DbCommand cmd = GetStoredProcCommand("Model_Method1");
using (IDataReader dr = DB.ExecuteReader(cmd))
{
LoadEntityBaseList(retval, dr, PopulateDomain);//populate list based on reader
}
return retval;
}
}
public class Manager
{
private readonly IDAL m_DAL;
public Manager()
{
ObjectFactory.Initialize(x => x.ForRequestedType<IDAL>());
m_DAL = ObjectFactory.GetInstance<IDAL>();
}
public List<Domain> Method1(int id)
{
return m_DAL.Method1(id);
}
}
[TestFixture]
public class ManagerTests
[Test]
public void Method1_Calls_DAL()
{
var list = new List<Model>();
using (m_mock.Record())
{
Expect.Call(_dal.Method1(1)).IgnoreArguments().Return(list);
}
using (m_mock.Playback())
{
Manager manager = new Manager();
var actual = manager.Method1(1);
Assert.That(actual, Is.Not.Null);
}
}
}
如果我包含 StructureMap 配置它会忽略 Mock 并且 Rhino.Mocks.MockRepository.VerifyAll() 会引发异常。
如果我不包含结构映射配置,我会得到“没有为 PluginFamily MyObject.IDAL 定义的默认实例”。
有人可以指出我做错了什么的正确方向吗?
I'm looking for some examples on how to do the following Mock Tests using StructureMap or Unity with NUnit.
I have the following code structure
public interface IDAL
{
List<Model> Method1(int id);
}
public class DAL : IDAL
{
public List<Model> Method1(int id)
{
List<Model> retval = new List<Model>();
DbCommand cmd = GetStoredProcCommand("Model_Method1");
using (IDataReader dr = DB.ExecuteReader(cmd))
{
LoadEntityBaseList(retval, dr, PopulateDomain);//populate list based on reader
}
return retval;
}
}
public class Manager
{
private readonly IDAL m_DAL;
public Manager()
{
ObjectFactory.Initialize(x => x.ForRequestedType<IDAL>());
m_DAL = ObjectFactory.GetInstance<IDAL>();
}
public List<Domain> Method1(int id)
{
return m_DAL.Method1(id);
}
}
[TestFixture]
public class ManagerTests
[Test]
public void Method1_Calls_DAL()
{
var list = new List<Model>();
using (m_mock.Record())
{
Expect.Call(_dal.Method1(1)).IgnoreArguments().Return(list);
}
using (m_mock.Playback())
{
Manager manager = new Manager();
var actual = manager.Method1(1);
Assert.That(actual, Is.Not.Null);
}
}
}
If I do include the StructureMap configuration It ignores the Mock and Rhino.Mocks.MockRepository.VerifyAll() throws an exception.
If I don't include the structureMap configuration I get No Default Instance defined for PluginFamily MyObject.IDAL.
Can someone point me in the right direction on what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ObjectFactory.Initialize 在应用程序的生命周期中只能调用一次,因此从类构造函数中调用它是没有意义的。
将 ObjectFactory.Initialize() 方法放在程序开头的某个位置(对于 Web,为 Main() 或 Application_Start()),然后更改 Manager,以便它采用 IDAL 作为构造函数参数。
然后在单元测试中,您根本不使用 StructureMap - 您只需将模拟的 IDAL 传递给 Manager。
ObjectFactory.Initialize should only be called once during the lifetime of your application, so it does not make sense to call it from a class constructor.
Put the ObjectFactory.Initialize() method somewhere in the beginning of your program (Main() or Application_Start() for the web) then change Manager so that it takes an IDAL as a constructor argument.
Then in your unit test, you do not use StructureMap at all - you just pass in a mocked IDAL to Manager.