在量角器中嘲弄角度服务 [英] Mocking an angular service in Protractor
问题描述
我正在尝试使用Protractor的addMockModule在我的end-2-end测试中插入模拟数据。
I'm trying to use Protractor's addMockModule to insert mock data in my end-2-end test.
我的测试应该去一个网站,通过css-class找到一个按钮并单击它。按钮单击调用MyService中的函数dostuff(),该函数从后端获取数据。
My test is supposed to go to a web site, find a button by css-class and click it. The button click calls the function dostuff() in MyService, which fetches data from the backend.
到目前为止我的代码:
describe('should display validation error', function () {
it('should work', function () {
browser.get('http://my.url');
browser.addMockModule('MyService', function () {
// Fake Service Implementation returning a promise
angular.module('MyService', [])
.value({
dostuff: function () {
return {
then: function (callback) {
var data = { "foo": "bar" };
return callback(data);
}
};
}
});
});
var button = element(by.css('.btn-primary'));
button.click();
browser.sleep(5000);
});
});
测试是访问网站并单击按钮。问题是显示来自数据库的真实数据,而不是模拟数据。
我跟着几个线程,就像这个:如何模拟angular.module('myModule',[])。在Jasmine / Protractor中的value()
The test is accessing the web site and clicking the button. The problem is that real data from the database is displayed, not the mock data. I followed several threads, like this one: How to mock angular.module('myModule', []).value() in Jasmine/Protractor
然而,它似乎是函数protractor.getInstance ()已弃用。
However, it seems like the function protractor.getInstance() is deprecated.
任何人都有这个工作吗?
Anyone got this working?
推荐答案
查看 addMockModule()
的单元测试。在调用 browser.get()之前尝试添加addMockModule语句
Take a look at the unit test for addMockModule()
. Try to add the addMockModule statement before you call browser.get()
这篇关于在量角器中嘲弄角度服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!