0

JsMockito的验证机制对我不起作用。我的设置是我有 2 个类,Common 和 Suggestions。

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/。请指教。

4

1 回答 1

0

你在这里有几个错误。

  1. 你已经模拟了你的测试系统。您正在测试 MyNS.Suggestions,对吗?因为它是一个模拟,“init()”方法是一个空存根。您需要使用 MyNS.Suggestions 对象的真实实例。
  2. 您正在调用“mockedSuggestions.setCommon(mockedCommon)”。因为“setCommon”是一个模拟方法,所以它什么也不做。如果你想引用一个模拟对象的值,你需要创建一个getter方法(“getCommon”),然后执行“when(mockedSuggestions).getCommon().thenReturn(mockedCommon);”。当然,这无关紧要,因为 Suggestions 对象首先不应该是模拟对象。

希望这可以帮助!

于 2013-03-05T00:43:45.257 回答