问题标签 [fakeiteasy]

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 投票
2 回答
1894 浏览

fakeiteasy - FakeItEasy ReturnsLazily 不带 out 参数

我是使用 FakeItEasy 的新手,我第一次尝试时就卡住了。我想伪造的界面有这样的方法:

我想看看参数是如何传递的,所以我使用 ReturnsLazily。这是我的尝试:

这会编译,但运行时会生成此异常:

这是正确的,但我不知道如何添加 out 参数。如果我将 ReturnLazily 部分更改为:

它不会编译,我不明白错误:

对于像我这样的新手来说,这看起来不喜欢 4 个参数,也不明白如何处理“out”。有人可以解释一下我应该如何阅读这些错误吗?一个工作示例也将非常受欢迎:-)

非常感谢!

- - 编辑 - -

这似乎有效:

比我希望的可读性差一点,这是否接近 ReturnsLazily 的预期用途?

0 投票
1 回答
480 浏览

c# - CallBaseMethod () 是否适用于 FakeItEasy 中的虚拟属性?

我有简单的课

当我跑步时(FakeItEasy.1.13.1)

我收到一个错误

我必须

CallBaseMethod () 是否适用于虚拟财产?我究竟做错了什么?

0 投票
1 回答
1750 浏览

asp.net-mvc - 需要对返回 RedirectResult 和锚标记的控制器的 ActionMethod 进行单元测试

这是控制器的返回语句:

所以,在我的测试方法中,我需要分配控制器的(sut)UrlHelper,这样我就不会得到空引用异常。我正在使用 FakeItEasy 来模拟 HttpContext:

这似乎工作正常,但是当我想证明控制器的操作方法已重定向到正确的方法和控制器时:

当控制器操作返回 RedirectToRouteResult 时,我无法提取 RouteValues。又名:

b/c RedirectResult 不允许我访问这些变量。另外,如果我尝试投射它:

我得到一个铸造错误。

我需要能够测试控制器是否重定向到适当的位置,但我不能这样做 b/c Url.Action(...) 必须在内部与其他项目(如 RouteData、RouteCollection 等)一起工作......

任何人都知道如何使用 ControllerContext 预填充我在测试中需要的“路由”数据:

为了让 Url.Action 在测试中将正确的值吐回给我?

谢谢!麦克风

0 投票
2 回答
5635 浏览

mocking - 任何关于 FakeItEasy 的好教程

我们正在考虑使用 FakeItEasy 作为我们的模拟框架。

为了在 FakeItEasy 上为团队举办研讨会,我正在寻找一个很好的教程来详细解释这个框架。我尝试查看 github 上的文档,但发现它非常简洁。

如果你知道一个很好的精心制作的页面/视频,你能指点我吗。谢谢。

0 投票
2 回答
344 浏览

c# - 禁用 FakeItEasy 的 AutoMocking

说我有一个界面

所以当我用 FakeItEasy 创建一个实例时:

所有属性(表)都有一个默认的假值。我可以明白为什么这在大多数情况下都很方便,但在我的情况下,我需要所有这些null

有没有聪明的方法呢?

0 投票
0 回答
168 浏览

c# - 如何添加表达式> 作为对伪造对象的拦截?

如果方法和参数已经表示为,是否有另一种方法来拦截 FakeItEasy 中的方法调用Expression<Action<T>>

通常我会简单地使用

但在我目前的情况下,我有一个假IFoo和一个Expression<Action<IFoo>>,并试图将两者结合在一起。

这实际上是我正在为 FakeIEasy 重写的现有 Moq 代码,但我不确定它是否可能。这个的起订量版本是

我尝试了明显的(如下),但得到了“指定的对象未被识别为假对象”错误(我怀疑是因为根本没有引用假对象!)

我会冒险猜测这可以通过实现IFakeObjectCallRule和使用来实现,Fake.GetFakeManager(foo).AddRuleFirst(customRule)但我想知道是否有更直接的方法来做到这一点?

0 投票
1 回答
991 浏览

c# - FakeItEasy Reset 对对象的调用

我使用 fake 很容易为我的对象生成假货。现在我测试是否调用了假对象上的某些方法。问题是我希望每个单元测试都将假的状态重置回原始状态。

我需要这个,因为我为每个单元测试类生成一次假货,所以我需要以某种方式重置调用状态。

这就是我的代码的样子:

0 投票
1 回答
1733 浏览

c# - 完全运行时单元测试失败 - API 限制:程序集已从不同位置加载

我有一套单元测试,如果我一起运行(使用 resharper)我会收到这个错误:

SetUp:System.TypeInitializationException:“FakeItEasy.Core.FakeScope”的类型初始化程序引发了异常。----> System.IO.FileLoadException:API 限制:程序集 'file:///C:\Users\abrown\Documents\Repos\ink.services.jetstar\My.Namespace.Tests.Unit\bin\Debug \My.Namespace.dll' 已从其他位置加载。它不能从同一 appdomain 中的新位置加载。

如果我单独运行它们,它们就会通过。

在异常的下方,它在这样的行上失败了:

编辑

如果我使用“标准”NUnit Test Runner,也会发生这种情况。

正如@david-arno 在评论中所说,通过更改 ReSharper 运行器中的设置,可以修复它(显然仅用于重新锐化)

为了完整起见,这是一个完整的堆栈跟踪

My.CompanyNamespace.ProjectName.Tests.Unit.DownloadAndStoreContentTests.gets_content_downloader_for_each_section_in_index:SetUp:System.TypeInitializationException:“FakeItEasy.Core.FakeScope”的类型初始化程序引发异常。----> System.IO.FileLoadException:API 限制:程序集 'file:///C:\Users\abrown\Documents\Repos\My.CompanyNamespace\My.CompanyNamespace.ProjectName.Tests.Unit\bin\Debug \My.CompanyNamespace.ProjectName.dll' 已从其他位置加载。它不能从同一 appdomain 中的新位置加载。

在 FakeItEasy.Core.FakeScope.get_Current() 在 FakeItEasy.IoC.DictionaryContainer.<>c_DisplayClass1 1.b 1.<Register>b__0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) at FakeItEasy.ServiceLocator.Resolve[T]() at FakeItEasy.RootModule.<RegisterDependencies>b__3(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass1_0 (DictionaryContainer c) 在 FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) 在 FakeItEasy.ServiceLocator.ResolveT 在 FakeItEasy。 RootModule.b_ 1a(DictionaryContainer c) 在 FakeItEasy.IoC.DictionaryContainer.<>c _DisplayClass11.<Register>b__0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) at FakeItEasy.ServiceLocator.Resolve[T]() at FakeItEasy.RootModule.<RegisterDependencies>b__19(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass11.b__0(DictionaryContainer c) 在 FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) 在 FakeItEasy.ServiceLocator.ResolveT 在 FakeItEasy.A.FakeT 在 My.CompanyNamespace.ProjectName.Tests.Unit.DownloadAndStoreContentTests.SetUp() 在 c: \Users\abrown\Documents\Repos\My.CompanyNamespace\My.CompanyNamespace.ProjectName.Tests.Unit\DownloadAndStoreContentTests.cs:line 23 --FileLoadException at System.Reflection.RuntimeAssembly。在 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName (AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly stackCrawlMark & StackMarkAssembly, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean for Introspection, Boolean suppressSecurityChecks) IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean for Introspection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark & stackMark, Boolean throwOnFileNotFound, Boolean for Introspection, Boolean suppressSecurityChecks.RuntimeAs) 在 System.RefleInternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean for Introspection, Boolean suppressSecurityChecks, StackCrawlMark & stackMark) at System.Reflection.Assembly.ReflectionOnlyLoadFrom(String assemblyFile) at FakeItEasy.Core.ApplicationDirectoryAssembliesTypeCatalogue.GetAllAvailableAssemblies() at FakeItEasy FakeItEasy.ImportsModule.b 上的 Core.ApplicationDirectoryAssembliesTypeCatalogue..ctor()GetAllAvailableAssemblies() 在 FakeItEasy.Core.ApplicationDirectoryAssembliesTypeCatalogue..ctor() 在 FakeItEasy.ImportsModule.bGetAllAvailableAssemblies() 在 FakeItEasy.Core.ApplicationDirectoryAssembliesTypeCatalogue..ctor() 在 FakeItEasy.ImportsModule.b_1(DictionaryContainer c) 在 FakeItEasy.IoC.DictionaryContainer.SingletonResolver 1.UnresolvedState.Resolve(DictionaryContainer container) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass11.b_ 0(DictionaryContainer c) 在 FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) 在 FakeItEasy.ServiceLocator.ResolveT 在 FakeItEasy.ImportsModule.b _0(DictionaryContainer c)在 FakeItEasy.IoC.DictionaryContainer.SingletonResolver 1.UnresolvedState.Resolve(DictionaryContainer container) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass11.b_0 (DictionaryContainer c) 在 FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) 在 FakeItEasy.ServiceLocator.ResolveT 在 FakeItEasy.ImportsModule.b _4[T](DictionaryContainer c) 在 FakeItEasy .IoC.DictionaryContainer.SingletonResolver1.UnresolvedState.Resolve(DictionaryContainer container) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass11.b__0(DictionaryContainer c) 在 FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) 在 FakeItEasy.ServiceLocator.ResolveT 在 FakeItEasy.Core.FakeScope.RootScope..ctor() 在 FakeItEasy.Core.FakeScope..cctor()

0 投票
2 回答
3703 浏览

c# - 如何使 FakeItEasy 伪造对象的方法在第一次调用时抛出并在第二次调用时返回?

我有一个在循环中多次调用的方法foreach,每次都使用相同的参数值。

我正在尝试编写一个测试,以确保循环继续迭代,即使_myService.Foo()第一次抛出异常也是如此。

Moq中,我可以将调用链接在一起ReturnsThrows如下所示:

这将导致调用Foo抛出异常,但所有后续调用都将返回someResult。我的主要目标是确保将 try/catch 块包裹在我的 foreach 块内的代码的后半部分,以便即使发生异常,循环也会继续。

我怎样才能在FakeItEasy中完成与此类似的事情?或者我可以使用不同的(更好的)策略来进行这种断言吗?

0 投票
2 回答
4482 浏览

c# - 如何使用 FakeItEasy 伪造一个动作<>

我正在使用 FakeItEasy 库为我的单元测试创​​建假货。

我有一个ClassUnderTest我想测试该方法的MethodToTest(Data dataObject)。此方法正在调用我想伪造的接口的方法:

在我的单元测试中,我想检查测试方法是否正确调用了接口(使用正确的属性值):

但是在这个测试中配置有问题。我得到了例外:

测试方法TestProject1.UnitTest1.TestMethod1抛出异常:FakeItEasy.ExpectationException:调用严格假的未配置方法“Execute”。

有人知道我必须如何CallTo()正确配置语句吗?