最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Variable in outer describe block is undefined when accessing in inner describe block with Mocha test - Stack Overfl

programmeradmin7浏览0评论

I've got a test suite that looks like the below:

(Notice the accountToPost variable at the top (below the first describe block)

describe('Register Account', function () {

    var accountToPost;

    beforeEach(function (done) {
        accountToPost = {
            name: 'John',
            email: '[email protected]',
            password: 'password123'
        };

        done();
    });

    describe('POST /account/register', function(){

        describe('when password_confirm is different to password', function(){

            //accountToPost is undefined!
            accountToPost.password_confirm = 'something'; 

            it('returns error', function (done) {
              //do stuff & assert
            });
        });
    });
});

My issue is that when I try to modify accountToPost in my nested describe block, it's undefined...

What can I do to fix this?

I've got a test suite that looks like the below:

(Notice the accountToPost variable at the top (below the first describe block)

describe('Register Account', function () {

    var accountToPost;

    beforeEach(function (done) {
        accountToPost = {
            name: 'John',
            email: '[email protected]',
            password: 'password123'
        };

        done();
    });

    describe('POST /account/register', function(){

        describe('when password_confirm is different to password', function(){

            //accountToPost is undefined!
            accountToPost.password_confirm = 'something'; 

            it('returns error', function (done) {
              //do stuff & assert
            });
        });
    });
});

My issue is that when I try to modify accountToPost in my nested describe block, it's undefined...

What can I do to fix this?

Share Improve this question asked Jan 31, 2014 at 0:22 AlexAlex 38.5k54 gold badges213 silver badges339 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 20

Keep the assignment where it is but wrap in in a beforeEach callback and your code will execute:

beforeEach(function () {
    accountToPost.password_confirm = 'something';
});

Mocha loads your file and executes it, which means the describe calls are executed right away before Mocha actually runs the test suite. That's how it figures out the set of tests you've declared.

I typically put nothing more than function and variable declarations in the body of the callbacks I pass to describe. Everything that changes the state of objects used in testing belongs in before, beforeEach, after or afterEach, or inside the tests themselves.

Another thing to know is that beforeEach and afterEach are executed before and after the callbacks to the it calls not the callbacks to the describe calls. So if you thought your beforeEach callback would execute before describe('POST /account/register', ... this is not correct. It executes just before it('returns error', ....

This code should illustrate what I am talking about:

console.log("0");
describe('level A', function () {
    console.log("1");
    beforeEach(function () {
        console.log("5");
    });

    describe('level B', function(){
        console.log("2");

        describe('level C', function(){
        console.log("3");

            beforeEach(function () {
                console.log("6");
            });

            it('foo', function () {
                console.log("7");
            });
        });
    });
});
console.log("4");

If you run mocha on this code, you'll see the numbers being output to the console in increasing order. I've structured it the same way your test suite is structured, but with the addition of my remended fix. The numbers 0 to 4 are output while Mocha is figuring out what tests are present in the suite. The testing has not begun yet. The other numbers are output during testing proper.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论