草庐IT

mocking-stubbing

全部标签

c# - 使用 stub 和模拟的正确方法是什么?

这是我的例子:[TestMethod]publicvoidNewAction_should_return_IndexAction(){NewViewModelviewModel=newNewViewModel(){Name="JoséInácioSantosSilva",Email="joseinacio@joseinacio.com",Username="joseinacio"};//IsUserRegisteredisusedtovalidateUsername,Usernameisunique._mockAuthenticationService.Setup(x=>x.IsUse

c# - 模拟 DataReader 并获取 Rhino.Mocks.Exceptions.ExpectationViolationException : IDisposable. Dispose();预期#0,实际#1

我正在尝试模拟一个SqlDataReaderSqlDataReaderreader=mocks.CreateMock();Expect.Call(reader.Read()).Return(true).Repeat.Times(1);Expect.Call(reader.Read()).Return(false);Expect.Call(reader.HasRows).Return(true);Expect.Call(reader.Dispose);Expect.Call(reader["City"]).Return("Boise");Expect.Call(reader["State

c# - 如何 stub HttpControllerContext

我正在尝试对从WebAPI(OData)Controller调用并接受HttpControllerContext的一段代码进行单元测试:publicstringMethodToTest(HttpControllerContextcontext){stringpub=string.Empty;if(context!=null){pub=context.Request.RequestUri.Segments[2].TrimEnd('/');}returnpub;}为了对此进行单元测试,我需要一个HttpControllerContext对象。我该怎么办?我最初试图用MicrosoftFak

c# - stub 接收到错误数据?

首先,在Win7/Win8等系统上执行代码没有问题,问题只存在于WindowsXP上。代码在一个按钮中,基本上运行taskmgr.exe作为另一个用户凭据(本地管理员凭据,这是一个KioskPC,它加载一个可以登录的C#应用程序,然后加载cmd.exe/taskmgr.exe作为不受链接到Kiosk用户的GPO影响的本地管理员)。但是,在XP上,单击按钮时出现错误:stub接收到错误数据。代码:privatevoidbtnTaskMgr_Click(objectsender,EventArgse){stringpassword="myPassword";SecureStringsecu

c# - 在不使用魔术字符串的情况下将参数传递给 FakeItEasy-mock?

我一直在使用Moq因为我过去几年的mock需要,但在看了FakeItEasy之后我想试一试。我经常想测试是否使用正确的参数调用了一个方法,但我发现使用FakeItEasy没有令人满意的方法。我有以下代码要测试:publicclassWizardStateEngine:IWizardStateEngine{privatereadonlyIWorkflowInvoker_workflowInvoker;privateList_history;publicWizardStateEngine(IWorkflowInvokerworkflowInvoker){_workflowInvoker=w

c# - 使用 rhino mocks 为每次调用返回不同的实例

我有这个代码:Expect.Call(factory.CreateOrder()).Return(newOrder()).Repeat.Times(4);当它被调用四次时,每次都返回相同的实例。我想要返回不同的实例。我希望能够做类似的事情:Expect.Call(factory.CreateOrder()).Return(()=>newOrder()).Repeat.Times(4);这可以通过某种方式完成吗? 最佳答案 而不是使用.Return(newOrder());尝试使用.Do((Func)delegate(){return

c# - stub 类的一个方法,让其他真正的方法使用这个 stub 的方法

我有一个TimeMachine类,它为我提供当前日期/时间值。该类看起来像这样:publicclassTimeMachine{publicvirtualDateTimeGetCurrentDateTime(){returnDateTime.Now;};publicvirtualDateTimeGetCurrentDate(){returnGetCurrentDateTime().Date;};publicvirtualTimeSpanGetCurrentTime(){returnGetCurrentDateTime().TimeOfDay;};}我想在我的测试中使用TimeMachin

c# - 如何设置 IConfigurationRoot 的 Mock 以返回值

这个问题在这里已经有了答案:Expressionreferencesamethodthatdoesnotbelongtothemockedobject(4个答案)关闭5年前。我已经使用IConfigurationRoute来访问这样的目录。if(type=="error")directory=_config.GetValue("Directories:SomeDirectory");_config是在构造函数中注入(inject)的IConfigurationRoot。我尝试了下面的方法来模拟它。varmockConfigurationRoot=newMock();mockConfig

c# - Rhino Mocks 的 "requires a return value or an exception to throw"是什么意思?

模拟对WCF服务的调用时,出现以下错误:Method'ICustomerEntities.GetCustomerFromPhoneNumber("01234123123");'requiresareturnvalueoranexceptiontothrow.我用谷歌搜索并在这里搜索-我能找到的只是我需要重新订购各种电话等,但在我的情况下这似乎没有意义?也许有人可以向我指出它实际上确实?我的测试设置是这样的_entities=MockRepository.GenerateStrictMock();并且第三行测试方法失败,设置result2_entities.Expect(ip=>ip.G

c# - 每次使用 Moq 调用方法时,如何使 Mock 返回一个新列表

我正在使用MOQ模拟具有预期返回列表的方法调用。我的方法返回一个列表,但我希望模拟在每次调用该方法时创建一个新列表。到目前为止我做了什么:Listexpected=newList{newCorrelation(){Code="SelfError1"},newCorrelation(){Code="SelfError2"}};MockselfMock=newMock();selfMock.Setup(f=>f.Validate()).Returns(expected);我想要实现的是让模拟在每次调用方法get时返回一个新列表。我试过这个但没用:selfMock.Setup(f=>f.Va