Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mvc4模拟设置等待操作_C#_Mongodb_Asp.net Mvc 4_Mocking - Fatal编程技术网

C# mvc4模拟设置等待操作

C# mvc4模拟设置等待操作,c#,mongodb,asp.net-mvc-4,mocking,C#,Mongodb,Asp.net Mvc 4,Mocking,我已经设置了工作单元模式和存储库模式,并且在其中使用了mongodb public class CourseRepository : ICourseRepository { private IMongoCollection<Course> _dbContext = null; public CourseRepository(IMongoCollection<Course> dbContext) { _dbContext = dbCo

我已经设置了工作单元模式和存储库模式,并且在其中使用了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);
    }
}
公共类课程存储库:ICourseRepository
{
私有IMongoCollection_dbContext=null;
公共课程定位(IMongoCollection dbContext)
{
_dbContext=dbContext;
}
公共异步任务添加(课程实体)
{
wait_dbContext.InsertOneAsync(实体);
}
}
在使用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();
}
[TestMethod]
公共异步任务TestMethod2()
{
var c=新的Mock();
c、 设置(x=>x.Add(新课程{CourseName=“asfd”,CourseId=2,CourseStatus=“Active”,CourseStartDate=System.DateTime.Now,CourseEndDate=System.DateTime.Now,CourseEndDate=System.DateTime.Now})).Verifiable();
c、 添加(新课程{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()
结果消息:
测试方法mvc4test.Tests.UnitTest1.TestMethod2引发异常:
Moq.MockVerificationException:以下设置不匹配:
ICourseRepository x=>x.Add()
结果跟踪:
在Moq.Mock.VerifyAll()时
在c:\Users\RM250443\Documents\Visual Studio 2013\Projects\mvc4test\mvc4test.Tests\UnitTest1.d\u b.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter.GetResult()中

有谁能帮我测试一个包含async、task和Wait的方法吗?

经过大量的研发,我发现我使用了两个课程实体实例,一个在设置中,一个在调用方法时,但应该这样做:

[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();
        }
[TestMethod]
公共异步任务TestMethod2()
{
var c=新的Mock();
课程a=新课程{CourseId=1};
c、 设置(x=>x.Add(a)).Verifiable();
c、 添加(a);
c、 VerifyAll();
}

经过大量的研发,我发现我使用了两个课程实体实例,一个在设置中,另一个在调用方法时,但应该这样做:

[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();
        }
[TestMethod]
公共异步任务TestMethod2()
{
var c=新的Mock();
课程a=新课程{CourseId=1};
c、 设置(x=>x.Add(a)).Verifiable();
c、 添加(a);
c、 VerifyAll();
}