简体   繁体   English

模拟一个接口{get;只有(Moq)

[英]Mocking an interface which is { get; } only (Moq)

I have an IUnitOfWork interface which contains mappings to all of our repositories, like so: 我有一个IUnitOfWork接口,它包含所有存储库的映射,如下所示:

public interface IUnitOfWork : IDisposable
{
    IRepository<Client> ClientsRepo { get; }
    IRepository<ConfigValue> ConfigValuesRepo { get; }
    IRepository<TestRun> TestRunsRepo { get; }
    //Etc...
}

Our IRepository class looks like this: 我们的IRepository类看起来像这样:

public interface IRepository<T>
{
    T getByID(int id);
    void Add(T Item);
    void Delete(T Item);
    void Attach(T Item);
    void Update(T Item);
    int Count();
}

My issue is that I'm trying to test a method that makes use of getById() , however this method is accessed through an IUnitOfWork object, like this: 我的问题是我正在尝试测试一个使用IUnitOfWork getById()方法,但是这个方法是通过IUnitOfWork对象访问的,如下所示:

public static TestRun getTestRunByID(IUnitOfWork database, int testRun)
{
    TestRun testRun = database.TestRunsRepo.getByID(testRun);
    return testRun;
}

In my test I have mocked 2 things; 在我的测试中,我嘲笑了两件事; IUnitOfWork and IRepository . IUnitOfWorkIRepository I have configured the IRepository so that it returns a TestRun item, however I can't actually make use of this repo since in the getTestRunByID() method it gets its own repo from the IUnitOfWork object. 我已经配置了IRepository以便它返回一个TestRun项,但是我实际上无法使用这个repo,因为在getTestRunByID()方法中它从IUnitOfWork对象获得它自己的repo。 As a result this causes a NullReferenceException . 结果,这会导致NullReferenceException

I have tried adding my repo to the IUnitOfWork's repo but it will not compile since all repos are marked as { get; 我已经尝试将我的repo添加到IUnitOfWork的repo但是它不会编译,因为所有repos都被标记为{get; } only. } 只要。 My test is: 我的测试是:

[TestMethod]
public void GetTestRunById_ValidId_TestRunReturned()
{
    var mockTestRunRepo = new Mock<IRepository<TestRun>>();
    var testDb = new Mock<IUnitOfWork>().Object;
    TestRun testRun = new TestRun();
    mockTestRunRepo.Setup(mock => mock.getByID(It.IsAny<int>())).Returns(testRun);

    //testDb.TestRunsRepo = mockTestRunRepo; CAN'T BE ASSIGNED AS IT'S READ ONLY

    TestRun returnedRun = EntityHelper.getTestRunByID(testDb, 1);     
}

How can I get my IUnitOfWork's repo to not throw a NullReferenceException ? 如何让我的IUnitOfWork's repo不抛出NullReferenceException

You can't assign to a mock, you need to configure the properties via a Setup. 您无法分配给模拟,您需要通过安装程序配置属性。


Instead of: 代替:

testDb.Setup(m => m.TestRunsRepo).Returns(mockTestRunRepo.Object);

Try: 尝试:

testDb.SetupGet(m => m.TestRunsRepo).Returns(mockTestRunRepo.Object);

or 要么

 testDb.SetupGet(m => m.TestRunsRepo).Returns(mockTestRunRepo.Object); 

I think you'll want something like this in your arrange: 我想在你的安排中你会想要这样的东西:

testDb.Setup(n => n.TestRunsRepo).Returns(mockTestRunRepo.Object);

You're trying to assign something to the mocks object when it's far easier to just set the mock up and have it return what you want that way. 当你更容易设置模拟并让它以你想要的方式返回时,你正试图为mocks对象分配一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM