StructureMap RhinoMock 记录/回放,需要示例

发布于 2024-08-11 23:01:47 字数 1530 浏览 5 评论 0原文

我正在寻找一些有关如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2024-08-18 23:01:48

ObjectFactory.Initialize 在应用程序的生命周期中只能调用一次,因此从类构造函数中调用它是没有意义的。

将 ObjectFactory.Initialize() 方法放在程序开头的某个位置(对于 Web,为 Main() 或 Application_Start()),然后更改 Manager,以便它采用 IDAL 作为构造函数参数。

ObjectFactory.Initialize(...)
var manager = ObjectFactory.GetInstance<Manager>();

public class Manager {
 public Manager(IDAL dal){
  m_DAL = dal;
 }
 ...
}

然后在单元测试中,您根本不使用 StructureMap - 您只需将模拟的 IDAL 传递给 Manager。

using (m_mock.Playback()) {
 Manager manager = new Manager(m_mock);

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.

ObjectFactory.Initialize(...)
var manager = ObjectFactory.GetInstance<Manager>();

public class Manager {
 public Manager(IDAL dal){
  m_DAL = dal;
 }
 ...
}

Then in your unit test, you do not use StructureMap at all - you just pass in a mocked IDAL to Manager.

using (m_mock.Playback()) {
 Manager manager = new Manager(m_mock);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文