- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个简单的需求,但我似乎很挣扎。
我创建了一个 stub
来模拟一个包含 Property
的 interface
:
public interface IMockIRuleRuningViewModel : IRuleRunningViewModel
{
int Id { get; set; }
}
mock
是:
var mock = MockRepository.GenerateStub<IMockIRuleRuningViewModel>();
现在我想模拟
一个我会为这个Property
放入setter的 Action ,这是我的尝试:
mock.Stub(x => x.Id).WhenCalled(
o =>
{
var engine = new RulesEngine(mock);
mock.ProcessRuleEngineResults(engine.RunRule("Id"));
});
但我不断收到此Exception
:
You are trying to set an expectation on a property that was defined to use PropertyBehavior. Instead of writing code such as this: mockObject.Stub(x => x.SomeProperty).Return(42); You can use the property directly to achieve the same result: mockObject.SomeProperty = 42;
最佳答案
以下对我有用:
HttpResponseBase response = MockRepository.GenerateMock<HttpResponseBase>();
// stub the getter
response.Stub(r => r.StatusCode).Return((int)HttpStatusCode.OK);
// Stub the setter
response.Stub(r => r.StatusCode = Arg<int>.Is.Anything).WhenCalled( o =>
{
Console.WriteLine("called");
});
因为我实际上想做的是模拟您可以获得但不能设置状态代码的情况(因为 header 已经发送),所以我不执行 WhenCalled()
,我这样做:
response.Stub(r => r.StatusCode = Arg<int>.Is.Anything)
.Throw(new HttpException("Server cannot set status after HTTP headers have been sent"));
您必须使用 MockRepository.GenerateMock
而不是 MockRepository.GenerateStub
。我不知道为什么。
关于c# - Rhino Mocks,设置 stub 属性后如何执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10687715/
我有一段代码是这样的: 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() { 预
我是一名优秀的程序员,十分优秀!