- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试测试一些现有类的逻辑。目前无法重构这些类,因为它们非常复杂并且正在生产中。
我想做的是创建一个模拟对象并测试一个方法,该方法在内部调用另一个很难模拟的方法。
所以我只想为辅助方法调用设置一个行为。
但是当我设置方法的行为时,方法的代码被调用但失败了。
我是不是遗漏了什么或者如果不重构类就无法测试?
我尝试了所有不同的模拟类型(Strick、Stub、Dynamic、Partial 等),但当我尝试设置行为时,它们最终都调用了方法。
using System;
using MbUnit.Framework;
using Rhino.Mocks;
namespace MMBusinessObjects.Tests
{
[TestFixture]
public class PartialMockExampleFixture
{
[Test]
public void Simple_Partial_Mock_Test()
{
const string param = "anything";
//setup mocks
MockRepository mocks = new MockRepository();
var mockTestClass = mocks.StrictMock<TestClass>();
//record beahviour *** actualy call into the real method stub ***
Expect.Call(mockTestClass.MethodToMock(param)).Return(true);
//never get to here
mocks.ReplayAll();
//this is what i want to test
Assert.IsTrue(mockTestClass.MethodIWantToTest(param));
}
public class TestClass
{
public bool MethodToMock(string param)
{
//some logic that is very hard to mock
throw new NotImplementedException();
}
public bool MethodIWantToTest(string param)
{
//this method calls the
if( MethodToMock(param) )
{
//some logic i want to test
}
return true;
}
}
}
}
最佳答案
MethodToMock 不是虚拟的,因此不能被模拟。使用部分模拟可以实现您想做的事情(我已经在与您类似的情况下完成了),但是您想要模拟的方法必须是接口(interface)实现的一部分或标记为虚拟。否则,您无法使用 Rhino.Mocks 模拟它。
关于c# - Rhino Mocks 部分模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2764929/
我有一段代码是这样的: while(count = inputStream.readLine()) != null) { //do something } 在单元测试用例中,我正在模拟 inp
我有一段代码是这样的: while(count = inputStream.readLine()) != null) { //do something } 在单元测试用例中,我正在模拟 inp
假设我在文件中具有以下命名导出 customer.ts export const saveDetails = ()=>{} export const loadDetails = ()=>{} 假设我在
我一直试图绕过dart的模拟库,但似乎我仍然不明白。 在我的库中,我有一个对外部资源的HTTP请求,我想以此模拟它不要一直依赖外部资源。 我的库中的主类如下所示: SampleClass(String
使用 postman 模拟服务器存在问题。它不响应具有路由参数的请求。例如,我的 uri 如下所示: PUT : {{server_url}}/order/{id} 但是当我以这种方式调用模拟服务器时
将任何转换器或空属性转换添加到 Jest 配置时,模拟无法正常工作。下面是简单的代码。 Jest 配置: "transform": { any regex: any transformer } 模块说
我最近一直在做一个项目,该项目已经开始变得相当依赖,并且一直在探索使用 AutoMocking 容器来清理我的测试并使其不那么脆弱的想法。 我听说过 TDD/BDD 纯粹主义者反对使用它们的论点,例如
有谁知道为什么 UsernameExists 不会返回 True。我的语法一定在某个地方搞砸了。 [TestMethod()] public void GenerateUsername
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
我得到一个 Moq 对象以在连续调用方法时返回不同的值。这是通过此扩展方法完成的: public static void ReturnsInOrder(this ISetup setup, param
rhino-mocks stub 和这里的期望有什么区别:在我看来它们的行为完全相同? mockContext.Stub(x => x.Find()) .Return(new List()
我正在尝试模拟方法 extra_get() 的调用,该方法通常返回一个字典列表。据我从模拟docs了解,如果我想返回iterable,我应该设置side_effect参数。 client.extra_
在我的 CentOS 6.2 机器的/var/lib/mock 文件夹下,我可以看到目标构建操作系统中的所有初始应用程序。如果我想添加 JDK 作为额外的应用程序,我该怎么做?谢谢! 最佳答案 只需将
我正在寻找一种让 stub 的返回值取决于其输入的干净方法。 目前我正在使用以下方法,但效果不佳。 metadataLogic.Expect(x => x.GetMake(args.Vehicle1.
我正在设置调用构建和执行查询的方法的期望。我想询问所用参数的属性。这可能吗 using (mocks.Record()) { Expect.Call(connection.Retrieve(S
有没有人有一个使用 Rhino Mocks 和 NInject 的自动模拟容器的实现? 最佳答案 好的,我使用 Moq 集成作为起点自己构建了一个。这很简单。你需要这 3 个类: public cla
使用后@patch在我的装饰器上它不再起作用了。我想进行一个失败并引发异常的调用,以便我可以检查我的装饰器是否正在捕获此异常,并正在调用某个函数。 mock do_sth_in_db让它引发异常是很容
谁能告诉我为什么下面的测试没有失败? [Test] public void uhh_what() { var a = MockRepository.GenerateMock(); a.
我找不到 Moq 和 Rhino 的具体功能比较。所有的问题都是“你更喜欢哪个以及为什么”,或者“这是你如何在 rhino 中进行简单的模拟以及如何在最小起订量中完成的”。 我在任何地方都找不到深入的
我正在尝试模拟数据存储库对象,但在 MockRepository 上设置期望后,它每次都返回 null。我的代码如下: [测试] 公共(public)无效 GetById_NotNull() { 预
我是一名优秀的程序员,十分优秀!