pengaturan tiruan mvc4 menunggu operasi

Saya memiliki unit pengaturan pola kerja bersama dengan pola repositori dan saya menggunakan mongodb di dalamnya.

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

saat menulis kasus uji unit menggunakan moq saya telah menulis kode

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

Saya mendapatkan kesalahan berikut:

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()

Adakah yang bisa membantu saya cara menguji metode yang terdiri dari async, tugas, dan menunggu.


person Ravi Modi    schedule 11.08.2015    source sumber


Jawaban (1)


Setelah banyak R&D saya menemukan bahwa saya menggunakan dua instance tentu saja entitas, satu dalam pengaturan dan satu lagi saat memanggil metode tetapi harus dilakukan seperti ini:

[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