Mockito验证机制不起作用

时间:2013-02-20 15:34:38

标签: javascript unit-testing mocking

JsMockito的验证机制对我不起作用。我的设置是我有2个课程,共同和建议。

MyNS.Common = function() {};
MyNS.Suggestions = function() {};

我在建议中设置了一个Common的实例。

MyNS.Suggestions.prototype.setCommon = function(common) {this.common = common;};

然后我使用Common.getAdGroupId()的返回值并使用此值调用Suggestions.refresh()。这就是我想要测试的一切。

MyNS.Suggestions.prototype.init = function() {
    // This is mocked to return 56 as can be seen in the test above.
    var adGroupId = this.common.getAdGroupId();
    this.refresh(adGroupId);
};

完整的工作示例是一个小提琴:http://jsfiddle.net/sbel/kqdTV/2/。请指教。

1 个答案:

答案 0 :(得分:0)

你在这里遇到了一些错误。

  1. 你已经嘲笑了你的系统。你正在测试MyNS.Suggestions,对吗?因为它是一个模拟,“init()”方法是一个空存根。您需要使用MyNS.Suggestions对象的实际实例。
  2. 你正在调用“mockedSuggestions.setCommon(mockedCommon)”。因为“setCommon”是一个模拟方法,所以它什么都不做。如果要在模拟对象上引用值,则需要执行类似创建getter方法(“getCommon”)的操作,然后执行“when(mockedSuggestions).getCommon()。thenReturn(mockedCommon);”。当然,这是无关紧要的,因为建议对象首先不应该是模拟。
  3. 希望这有帮助!

相关问题