问题标签 [moq-3]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
96 浏览

c# - 我的 Moq 模拟未在我的测试方法中被接受

当我创建我的 moq 模拟并尝试将其传递给我的类构造函数时,我收到以下消息:Argument type Moq.Mock<...mockIAppCache> is not assingable to paramter type 'IAppCache'。我包含了该库,并且可以找到对 Mock() 的引用。我在这里错过了什么吗?

0 投票
2 回答
661 浏览

c# - 我将如何重构一个静态方法以便我可以测试我的方法?

我知道我不能使用 Moq 在我的被测方法中模拟静态方法调用,那么我需要做什么来重构该方法以便我可以测试它?我也有一个调用基类方法的方法,我需要重构它吗?如果需要,如何重构?我不想使用 MS.Fakes 或 TypeMocks 来创建 shim,我宁愿重构并编写可靠的代码!

0 投票
1 回答
633 浏览

c# - How to pass a List parameter using Moq

I am trying to mock a method which takes two parameters, the signature of method looks as follows:

I want this method to return something only if the 'newRoleList' parameters contains some predefined roles and a specific username in it. So instead of using It.IsAny<> I'm trying to use It.Is<>. The problem I am facing is with the 2nd parameter. How can I setup this 2nd parameter?

I'm trying to achive something like this:

But this is always returning 'null'. If I change the 2nd parameter to It.IsAny<List<Role>>() then it's returning me a proper value.

Please suggest how to achieve this, is there any better way for supplying a specific list as parameter or what?

0 投票
1 回答
1790 浏览

c# - 模拟 LINQ 表达式 - Moq

如何使用 Moq 模拟 - Expression> 的内容?

我正在尝试模拟对我的 repo 层的调用,该调用采用 LINQ 表达式来构造查询。我正在尝试以下语法,但失败了。SearchFor 方法不会被调用。

这里的GetEmployees方法如下所示。

0 投票
1 回答
1008 浏览

moq - 期望多次调用方法

如何告诉 Moq 期待多个电话,以便我仍然可以使用MockRepositoryto VerifyAll,如下所示?

我知道您可以在验证时执行此操作,但随后我必须独立验证所有内容。有没有告诉它会发生什么,而不是在事件发生后验证它?

类似于:

基本上,我想在测试开始时设定我所有的期望,而不是将它们放在多个地方。

0 投票
3 回答
13500 浏览

unit-testing - how do you mock an xml for unit testing?

I need to unit testing this GetData method.

this method needs a XmlElement parameter. how do I mock it? in my code, I first created a xmlDocument, then load the xml file.

for me to test it, first i need to create a xml file without employeeNo, the create antoher one without name, maybe alot more for other scenarios. it just seems like alot work. is there a better way to test it?

should I use moq or other testing framework to simplify the testing?

0 投票
1 回答
2968 浏览

c# - 带有 AutoMoq 和具体对象注入的 AutoFixture

我面临一个与具体类的自动模拟相关AutoFixture以及它如何处理的奇怪问题。AutoMoqCustomization我怀疑我没有很好地使用它,但想知道问题出在哪里。首先是她的一些背景。假设我有一个要测试的课程:

及其依赖类TenantProvisioningService(TenantProvisioningService 的依赖在这里不相关,因为它们会被自动模拟,我在测试中并不关心):

这是我的简单测试:

这不起作用,因为当我fixture.Create<IdentityApplicationService>()在其构造函数中调用 then 时,会注入一个具体的 TenantProvisioningService 而不是您可以在mockTenantProvisioningService.Object中找到的代理。

如果我像这样重写测试(注意夹具注入线),一切都会按预期工作(至少由我 :))

所以我的问题是:我做错了还是它应该是这样?如果不是,请给我解释为什么 AutoFixture 会这样。

0 投票
1 回答
95 浏览

c# - 在 TestMethods 中使用 Class Scoped vars 很危险

我对最小起订量和 TDD/单元测试都是新手。我发现自己在每个测试方法的“排列”部分重复了很多相同的代码,似乎真的应该有更好的方法。

首先是经常重复的代码:

我考虑过制作自定义片段、常量等,但这并不能真正节省重复代码,只是节省了我一些打字时间。

接下来,我考虑在类级别上对以上所有内容进行范围...但是一些警告铃声响起...如果该类为所有测试方法实例化一次,那么可能存在变量污染吗?鉴于这里的小样本确实不会发生,但我正在努力提前思考/养成良好的做法和习惯。

如何创建一个在该对象中定义了所有这些的“模拟和身份验证”对象。然后我可以拥有几个具有不同凭据的对象,并且我的测试方法可以仅引用/使用所需的一种变体?

那么对于将一些 DRY 原则应用于我的测试用例,您有什么建议/建议。

0 投票
1 回答
981 浏览

unit-testing - 如何使用 moq 框架为域驱动设计架构中的存储库层编写单元测试

如何使用 moq 框架为域驱动设计架构中的存储库层编写单元测试?我的存储库类如下。

在这个存储库类中,哪些被模拟了?请举一些例子。

0 投票
1 回答
52 浏览

asp.net-mvc - 在 MVC 中 MOQing 一个控制器

我有以下控制器

IProviderRepository 看起来像这样。

我显然必须对控制器进行最小起订量。我猜这意味着我必须对方法进行 MPOQ。所以,我使用以下方法创建了一个测试项目。

有人可以指导我在这里究竟需要模拟什么。我觉得我有点过头了。