การตั้งค่าจำลอง mvc4 กำลังรอการดำเนินการ

ฉันมีหน่วยการตั้งค่ารูปแบบการทำงานพร้อมกับรูปแบบพื้นที่เก็บข้อมูลและฉันใช้ mongodb ในนั้น

public class CourseRepository : ICourseRepository
{
    private IMongoCollection<Course> _dbContext = null;
    public CourseRepository(IMongoCollection<Course> dbContext)
    {
        _dbContext = dbContext;
    }
    public async Task Add(Course entity)
    {
        await _dbContext.InsertOneAsync(entity);
    }
}

ในขณะที่เขียน unit test case โดยใช้ moq ฉันได้เขียนโค้ดแล้ว

[TestMethod]
public async Task TestMethod2()
{
       var c = new Mock<ICourseRepository>();
       c.Setup(x => x.Add(new Course { CourseName = "asfd", CourseId = 2, CourseStatus = "Active", CourseStartDate = System.DateTime.Now, CourseEndDate = System.DateTime.Now, CourseEntryDate = System.DateTime.Now })).Verifiable();
       c.Object.Add(new Course { CourseName = "asfd", CourseId = 2, CourseStatus = "Active", CourseStartDate = System.DateTime.Now, CourseEndDate = System.DateTime.Now, CourseEntryDate = System.DateTime.Now });
       c.VerifyAll();
}

ฉันได้รับข้อผิดพลาดต่อไปนี้:

Result Message: 
Test method mvc4test.Tests.UnitTest1.TestMethod2 threw exception: 
Moq.MockVerificationException: The following setups were not matched:
ICourseRepository x => x.Add()
Result StackTrace:  
at Moq.Mock.VerifyAll()
   at mvc4test.Tests.UnitTest1.<TestMethod2>d__b.MoveNext() in c:\Users\RM250443\Documents\Visual Studio 2013\Projects\mvc4test\mvc4test.Tests\UnitTest1.cs:line 37
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

ใครก็ได้โปรดช่วยฉันด้วยว่าจะทดสอบวิธีการที่ประกอบด้วย async งานพร้อมกับการรอคอยได้อย่างไร


person Ravi Modi    schedule 11.08.2015    source แหล่งที่มา


คำตอบ (1)


หลังจากการวิจัยและพัฒนามากมาย ฉันพบว่าฉันใช้เอนทิตีของหลักสูตรสองอินสแตนซ์ หนึ่งอินสแตนซ์ในการตั้งค่าและอีกหนึ่งอินสแตนซ์ขณะโทร แต่ควรทำเช่นนี้:

[TestMethod]
        public async Task TestMethod2()
        {
            var c = new Mock<ICourseRepository>();
            Course a = new Course { CourseId = 1 };
            c.Setup(x => x.Add(a)).Verifiable();
            c.Object.Add(a);
            c.VerifyAll();
        }
person Ravi Modi    schedule 12.08.2015