- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经为我的服务层构建了单元测试。我没有使用 Mock,因为我认为既然你正在添加/删除/查询数据库,为什么要查询 mock 因为结果可能不同,但这不是我要问的。
现在我正在使用 Moq 来测试我的 web api 层。我认为这很好,就好像我所有的测试都在服务层上通过一样,模拟服务来测试 web api 就可以了。
我已经设法为我的 GetAsync
方法编写了一个测试,它工作正常,就像这样
这是 Controller :
public async Task<IHttpActionResult> GetAsync(long id)
{
Content content = await _service.GetAsync(id);
ContentModel model = Mapper.Map<ContentModel>(content);
return Ok(model);
}
这是测试:
[TestMethod]
public void Content_GetAsync()
{
// arrange
var mockService = new Mock<IContentService>();
mockService.Setup(x => x.GetAsync(4))
.ReturnsAsync(new Content
{
Id = 4
});
// setup automapper
AutoMapperConfig.RegisterMappings();
// act
var controller = new ContentController(mockService.Object);
var actionResult = controller.GetAsync(4).Result;
var contentResult = actionResult as OkNegotiatedContentResult<ContentModel>;
// assert
Assert.IsNotNull(contentResult);
Assert.IsNotNull(contentResult.Content);
Assert.AreEqual(4, contentResult.Content.Id);
}
我相信我写的是正确的,而且它似乎有效。现在我想测试我的 PostAsync
方法来添加一个项目。 Controller 看起来像这样:
public async Task<IHttpActionResult> PostAsync(ContentModel model)
{
Content content = Mapper.Map<Content>(model);
await _service.AddAsync(content);
return Created<ContentModel>(Request.RequestUri, Mapper.Map<ContentModel>(content));
}
这是测试:
[TestMethod]
public void Content_PostAsync()
{
var mockService = new Mock<IContentService>();
mockService.Setup(e => e.AddAsync(new Content()))
.ReturnsAsync(1);
// setup automapper
AutoMapperConfig.RegisterMappings();
// act
var controller = new ContentController(mockService.Object);
var actionResult = controller.PostAsync(new ContentModel {
Heading = "New Heading"
}).Result;
var contentResult = actionResult as CreatedAtRouteNegotiatedContentResult<ContentModel>;
// assert
Assert.IsNotNull(contentResult);
Assert.IsNotNull(contentResult.Content);
Assert.AreEqual("New Heading", contentResult.Content.Heading);
}
现在当我运行它时,我得到一个错误:
null reference exception. "Request" from the Request.RequestUri is null.
所以我更改了我的 Controller 并对此进行了测试,以尝试模拟它。
测试代码:
public Task<IHttpActionResult> PostAsync(ContentModel model)
{
return PostAsync(model, Request);
}
/// Unit testable version of above. Cannot be accessed by users
[NonAction]
public async Task<IHttpActionResult> PostAsync(ContentModel model, System.Net.Http.HttpRequestMessage request)
{
Content content = Mapper.Map<Content>(model);
await _service.AddAsync(content);
return Created<ContentModel>(request.RequestUri, Mapper.Map<ContentModel>(content));
}
Controller 代码:
[TestMethod]
public void Content_PostAsync()
{
// arrange
var mockRequest = new Mock<System.Net.Http.HttpRequestMessage>();
mockRequest.Setup(e => e.RequestUri)
.Returns(new Uri("http://localhost/"));
var mockService = new Mock<IContentService>();
mockService.Setup(e => e.AddAsync(new Content()))
.ReturnsAsync(1);
// setup automapper
AutoMapperConfig.RegisterMappings();
// act
var controller = new ContentController(mockService.Object);
var actionResult = controller.PostAsync(new ContentModel {
Heading = "New Heading"
}, mockRequest.Object).Result;
var contentResult = actionResult as CreatedAtRouteNegotiatedContentResult<ContentModel>;
// assert
Assert.IsNotNull(contentResult);
Assert.IsNotNull(contentResult.Content);
Assert.AreEqual("New Heading", contentResult.Content.Heading);
}
现在我收到一条错误消息:
Invalid setup on a non-virtual (overridable in VB) member: e => e.RequestUri
有人可以帮我解决这个问题吗?我确定我在所有测试中都正确使用了 Mock
,但单元测试对我来说是新的,所以也许我只是没有做正确的事情。
最佳答案
使用 Moq
你只能模拟 virtual/absrtact
成员。 RequestUri
不是 HttpRequestMessage
的虚拟成员,因此出现错误消息。
您应该能够直接新建一个 HttpRequestMessage
而无需模拟它并将其传入。
var request = System.Net.Http.HttpRequestMessage>();
request.RequestUri = new Uri("http://localhost/");
// act
var controller = new ContentController(mockService.Object);
var actionResult = controller.PostAsync(new ContentModel {
Heading = "New Heading"
}, request).Result;
关于c# - 使用 Moq 和 EF6 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28108296/
我有一个测试,我像这样传入一个对象: var repo = new ActualRepo(); var sut = new Sut(repo); 在我的测试中,Repo 有一个我需要实际执行的方法,而
想知道是否有可能将 Prism 事件聚合器最小化 让我们以他们拥有的EventAggregator快速入门为例 [TestMethod] public void Presente
我正在 mock MSMQ 的包装器。包装器只是允许创建一个直接调用 MessageQueue 类的静态方法的对象实例。 我想测试阅读队列是否筋疲力尽。为此,我希望模拟包装器返回一些好的结果,并在第四
我正在尝试开始使用最小起订量,但无法找到任何好的资源来做我需要的事情。 我有一个数据接口(interface)类,它有一个通过存储过程返回数据集的 Get 方法。这就是代码的编写方式,我目前无法更改它
我想使用 Moq 框架。我想下载框架,所以我到达了http://code.google.com/p/moq/通过谷歌,但第一行提到这个项目已转移到GitHub。当我去 GitHub 时,我只看到了源代
我想看看 Moq 是否是我想在新项目中使用的东西,因为我使用的其他模拟框架正在挑战恕我直言。例如,我有一个方法: IEnumerable GetPickLists(); 我不确定我应该如何 mock
这看起来很简单,但我似乎无法让它工作。 我有一个带有 Save 方法的类,它只调用另一个方法 ShouldBeCalled()。我想验证如果我调用 Save() 另一个方法 ShouldBeCalle
我正在修改一个类方法,它格式化一些输入参数日期,这些日期随后在方法调用中用作参数到基类(位于另一个程序集中)。 我想验证我传递给我的方法的日期在传递给基类方法时的格式是否正确,因此我想对基类方法调用进
我有以下方法: public CustomObect MyMethod() { var lUser = GetCurrentUser(); if (lUser.HaveAccess)
我有一个对象,我正在尝试使用最小起订量来模拟。该对象的构造函数具有必需的参数: public class CustomerSyncEngine { public CustomerSyncEng
我找不到对齐的 moq 和 moq contrib 版本。我可能只是谷歌失败了。 详情: 是否有适用于 moq 版本 4.0.10827 的 moq.contrib 版本,最新版本来自 http://
有没有人试过这个? 我喜欢最小起订量,我喜欢 pex 正在做的事情,但还没有一起尝试过。我认为在大多数情况下,我更喜欢使用 moq 而不是痣,但很想知道是否有人遇到了障碍? 他们玩得好吗? 最佳答案
VerABLE() 的目的是什么? 如果我验证 Mock 并将其保留,它仍然会验证 SetUp。 编辑:我使用的是VerifyAll(),因此所有内容都被验证。更改为 Verify() 后,仅检查我的
我在一个使用 Moq 和 AutoFixture.AutoMoq 的特定项目的构建服务器上运行单元测试时遇到问题。这是错误: System.IO.FileLoadException : Could n
我正在尝试创建一个具体类的模拟,并用另一个模拟来模拟它的一个属性。 public class MyClass { public virtual IAdapter Adapter {get; int
我无法使用 Moq (v.4.2) 在 protected 方法上设置回调。 代码如下所示: public abstract class AbstractClass { protected a
我在我的测试约定中使用 AutoMoqCustomization。 考虑下面的代码。一切都很好,直到我向其中一个具体类添加构造函数。当我这样做时,我得到“找不到无参数构造函数”。我们知道 AutoFi
在 Moq 中模拟 protected 虚拟(非通用)方法很容易: public class MyClass { .... protected virtual int MyMethod(D
如何验证是否使用 Moq 调用了“CallWithRef”方法? public interface ITest { void CallWithoutRef(string value, List
在我的单元测试中使用 Moq 生成 stub 和模拟,我有一个案例,我想验证是否调用了采用 Delegate 参数的方法。我不关心提供的特定 Delegate 参数,我只想确保该方法实际上被调用。该方
我是一名优秀的程序员,十分优秀!