惩戒方法结果 [英] Mocking method results

查看:126
本文介绍了惩戒方法结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法,一个从另一个方法中调用方法的假的结果。

I'm trying to find a way to fake the result of a method called from within another method.

我有一个要求独立的一个LoadData的方法。帮助得到一些数据,然后将其转换(我感兴趣的测试结果转化)

I have a "LoadData" method which calls a separate helper to get some data and then it will transform it (I'm interested in testing the transformed result).

所以我有这样的代码:

public class MyClass(){
  public void LoadData(){
    SomeProperty = Helper.GetSomeData();
 }
 public object SomeProperty {get;set;}
}

我想有从Helper.GetSomeData()方法的已知结果。我可以使用模拟框架(我已经得到了相当有限的经验,犀牛制品,但我开到任何东西)来强制期望的结果?如果是这样,如何

I want to have a known result from the Helper.GetSomeData() method. Can I use a mocking framework (I've got fairly limited experience with Rhino Mocks but am open to anything) to force an expected result? If so, how?

*编辑 - ?耶如预期我无法实现我想要的黑客,我不得不制定出一个更好的方式来设置数据。

*Edit - yeah as expected I couldn't achieve the hack I wanted, I'll have to work out a better way to set up the data.

推荐答案

据我所知,你应该为辅助对象的接口或基抽象类。随着犀牛嘲笑,那么你可以返回所需的值。

As far as I know, you should create an interface or a base abstract class for the Helper object. With Rhino Mocks you can then return the value you want.

此外,还可以作为参数,你通常从辅助对象检索数据增加对LoadData接受过载。这甚至可能会更容易。

Alternatively, you can add an overload for LoadData that accepts as parameters the data that you normally retrieve from the Helper object. This might even be easier.

这篇关于惩戒方法结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆