发布于 2017-08-12 01:20:01
到目前为止,你尝试过什么?您是否尝试过使用beforeEach重新初始化您正在使用的对象?你可以在你的测试之外声明共享变量。
编辑:添加Jerry所说的细节:如果您希望在同一个测试中重用所有变量,则必须使它们成为全局变量。请参见下面的示例
///include dependencies
var assert = require('assert'),
chai = require('chai'),
expect = chai.expect,
chakram = require('chakram');
//INIT GLOBAL VARAIBLES FOR within the same test
var testObj,
dummyData = {
user: 'John Kim',
lastSeenOnline: 'Wed August 11 12:05 2017'
};
describe ('#User', function () {
beforeEach(function () {
//init what the object contains
testObj = new DataStore(data, ContainerStore);
});
it ('#Should return the name of the user', function () {
assert.equal(testObj.get('user'), dummyData.user);
});
it("should offer simple HTTP request capabilities", function () {
return chakram.get("http://httpbin.org/get");
});
});注意:我使用react,但这只是一个示例。我们假设ContainerStore包含一个具有get()方法的方法,该方法只获取我们的JSON对象的值。您可以在不同的describe块中多次使用testObj,因为它是在测试之外声明的。但是您应该记住始终在beforeEach()中重新初始化您的tesObj;否则,您将冒着填充单个测试的风险。在某些情况下,您不必初始化beforeEach(),而且它是可选的。
发布于 2019-06-02 03:29:43
例如在config.js中
module.exports = {
"baseUrl": "http://example.com/",
"googleUrl": "http://www.google.com.tr/"
}; 并在javascript代码中使用:
let config = require('/config');
describle("test describle", () => {
it("test", () => {
chakram.get(config.baseUrl + "products"); //for example use
})
})https://stackoverflow.com/questions/45632146
复制相似问题