我最近遇到了一个无法解释的问题.我在这些测试中有很多代码,所以我会尽力在这里捕获这个想法
我的测试看起来像:
describe('main page', function(){
beforeEach(function(done){
addUserToMongoDb(done); // #1
});
afterEach(function(done){
removeUserFromMongoDb(done);
});
context('login', function(){
it('should log the user in, function(){
logUserIn(user_email); // #2 - This line requires the user from the beforeEach
});
});
context('preferences', function(){
before(function(done){ //#3
logUserInBeforeTest(user_email);
});
it('should show the preferences', function(){
doCheckPreferences(); // #4
});
});
});
Run Code Online (Sandbox Code Playgroud)
问题是,beforeEach #1
运行正常.我可以看到它发生在数据库和测试#2
传递中.
但是,首选项上下文中的测试#4
失败,因为它无法找到用户登录它们#3
.
似乎上下文before
在describe之前执行beforeEach
,导致它们失败.如果我移动logUserIn
到it
块中它工作正常.
什么可能导致这个?
Ste*_*ott 58
Mocha的测试运行器在Mocha Test Runner的Hooks部分解释了这个功能.
从钩子部分:
describe('hooks', function() {
before(function() {
// runs before all tests in this file regardless where this line is defined.
});
after(function() {
// runs after all tests in this file
});
beforeEach(function() {
// runs before each test in this block
});
afterEach(function() {
// runs after each test in this block
});
// test cases
});
Run Code Online (Sandbox Code Playgroud)
您可以将这些例程嵌套在其他描述块中,这些块也可以具有before/beforeEach例程.
小智 24
我发现了类似的问题.该文档具有误导性,因为"在此块之前"(至少对我来说)意味着"在此描述部分之前".同时它意味着"在任何描述部分之前".检查此示例:
describe('outer describe', function () {
beforeEach(function () {
console.log('outer describe - beforeEach');
});
describe('inner describe 1', function () {
before(function () {
console.log('inner describe 1 - before');
});
describe('inner describe 2', function () {
beforeEach(function () {
console.log('inner describe 2 - beforeEach');
});
});
// output will be:
// inner describe 1 - before
// outer describe - beforeEach
// inner describe 2 - beforeEach
Run Code Online (Sandbox Code Playgroud)
看来,在你的层次结构中放置它的位置并不重要before
- 它将在任何描述之前运行,而不是在其包含描述之前运行.
归档时间: |
|
查看次数: |
47469 次 |
最近记录: |