2017-09-05 77 views
0

我有問題,此測試:起訂量的DbContext與異步測試拋出錯誤

[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); 
    } 

我測試這個功能:

public async Task AddSingleChoiseTest(SingleChoiseTest singleChoiseTest) 
    { 
     if (singleChoiseTest == null) 
      throw new NullReferenceException("Test cannot be null."); 

     if (!singleChoiseTest.IsValid()) 
     { 
      throw new ArgumentException("Test is not vaild."); 
     } 

     using (_context) 
     { 
      _context.SingleChoiseTests.Add(singleChoiseTest); 
      await _context.SaveChangesAsync(); 
     } 
    } 

當我運行它拋出異常下面的測試: System.NotSupportedException:在非虛擬(VB中可重寫)成員上設置無效:m => m.SingleChoiseTests

回答

0

由於錯誤試圖告訴您,您只能撥打Setup()virtual成員。

您需要使SingleChoiseTests成爲虛擬。

+0

呵呵,我現在明白了,謝謝! –