Moq DbContext与异步测试抛出错误

时间:2017-09-05 18:09:27

标签: c# entity-framework nunit moq

我对此测试有疑问:

sudo apt-get update && sudo apt-get upgrade heroku

我正在测试这个功能:

    [Test]
    public async Task Add_async_vaild_test_entity_to_database_should_be_added()
    {
        // Setup Moq
        var mockSet = new Mock<DbSet<SingleChoiseTest>>();
        var mockContext = new Mock<NoezaTestContext>();
        mockContext.Setup(m => m.SingleChoiseTests).Returns(mockSet.Object);

        // Perform action
        var operations = new SingleChoiseTestOperations(mockContext.Object);
        await operations.AddSingleChoiseTest(new SingleChoiseTest("question", new Answer("d1", false),
            new Answer("d2", false), new Answer("d3", false), new Answer("d4", true)));

        mockSet.Verify(t => t.Add(It.IsAny<SingleChoiseTest>()), Times.Once);
        mockContext.Verify(m => m.SaveChangesAsync(), Times.Once);
    }

当我进行测试时,它会抛出异常: System.NotSupportedException:非虚拟(在VB中可覆盖)成员上的无效设置:m =&gt; m.SingleChoiseTests

1 个答案:

答案 0 :(得分:0)

由于错误试图告诉您,您只能为Setup()成员致电virtual

您需要SingleChoiseTests虚拟。