request-promise + co API 在量角器测试中触发
request-promise + co API trigger in protractor test
我想通过量角器测试调用 module.api.create。参考这个解决方案: -
我正在像这样使用请求承诺 + co:-
//api/module1.js
var co = require('co');
var rp = require('request-promise');
exports.create = co(function* def() {
var response, token;
urlLogin.body.username = username;
response = yield rp(urlLogin);
//extract token and run other APIs
...
}).catch(err => console.log);
和
//api/api.js
var module1= require('./module1'),
exports.module1= function (){
return module1;
};
在我的 Spec/Test 中添加
api = require('../../api/api');
api.module1.create;
我面临的问题是即使没有调用 "api.module1.create;" 行,require 行 "api = require('../../api/api');" 也会在每次执行测试时自动调用创建
来自co
README:
co@4.0.0 has been released, which now relies on promises. It is a stepping stone towards the async/await proposal. The primary API change is how co() is invoked. Before, co returned a "thunk", which you then called with a callback and optional arguments. Now, co() returns a promise.
我相信您正在寻找 co.wrap
,其中 returns 一个执行生成器的函数和 returns 一个 promise(这个函数也可能被称为 thunk)。仅使用 co
急切地执行生成器和 returns 执行生成器的结果。
const co = require('co')
co(function* () {
// this will run
console.log('hello from plain co!')
})
co.wrap(function* () {
// this won't run because we never call the returned function
console.log('hello from wrapped co 1!')
})
const wrappedfn = co.wrap(function* () {
// this runs because we call the returned function
console.log('hello from wrapped co 2!')
})
wrappedfn()
你也可以自己包装一个函数,它和co.wrap
做同样的事情,然后让你做更多的事情。
exports.create = function() {
return co(function* () {
// this will run only when exports.create is called
console.log('hello from plain co!')
})
// If you want to do stuff after and outside the generator but inside the enclosing function
.then(...)
}
我想通过量角器测试调用 module.api.create。参考这个解决方案: -
//api/module1.js
var co = require('co');
var rp = require('request-promise');
exports.create = co(function* def() {
var response, token;
urlLogin.body.username = username;
response = yield rp(urlLogin);
//extract token and run other APIs
...
}).catch(err => console.log);
和
//api/api.js
var module1= require('./module1'),
exports.module1= function (){
return module1;
};
在我的 Spec/Test 中添加
api = require('../../api/api');
api.module1.create;
我面临的问题是即使没有调用 "api.module1.create;" 行,require 行 "api = require('../../api/api');" 也会在每次执行测试时自动调用创建
来自co
README:
co@4.0.0 has been released, which now relies on promises. It is a stepping stone towards the async/await proposal. The primary API change is how co() is invoked. Before, co returned a "thunk", which you then called with a callback and optional arguments. Now, co() returns a promise.
我相信您正在寻找 co.wrap
,其中 returns 一个执行生成器的函数和 returns 一个 promise(这个函数也可能被称为 thunk)。仅使用 co
急切地执行生成器和 returns 执行生成器的结果。
const co = require('co')
co(function* () {
// this will run
console.log('hello from plain co!')
})
co.wrap(function* () {
// this won't run because we never call the returned function
console.log('hello from wrapped co 1!')
})
const wrappedfn = co.wrap(function* () {
// this runs because we call the returned function
console.log('hello from wrapped co 2!')
})
wrappedfn()
你也可以自己包装一个函数,它和co.wrap
做同样的事情,然后让你做更多的事情。
exports.create = function() {
return co(function* () {
// this will run only when exports.create is called
console.log('hello from plain co!')
})
// If you want to do stuff after and outside the generator but inside the enclosing function
.then(...)
}