Michael Hopkins
Michael Hopkins

Reputation: 11

Jasmine testing, using a constructor in the beforeEach

i am attempting to create a new instance of two classes that i have already written in separate files. when i try to create new instances of them in the beforeEach() section of the test code, the tests return undefined for my newly created objects. however when i create them in each IT section the test run perfectly.

describe("placeStone", function() {
beforeEach(function() {
    var go = new Display();
    var logic = new Internals(go);    
    logic.tempBoard = [ array];
});
it("should place a black stone at 0,6", function() {  
   logic.placeStone(logic.black,0,6);  
   expect(logic.tempBoard[6][0]).toEqual("B");
});

this returns logic undefined.

describe("placeStone", function() {

it("should place a black stone at 0,6", function() {  
   var go = new Display();
   var logic = new Internals(go);    
   logic.tempBoard = [ array];
   logic.placeStone(logic.black,0,6);  
   expect(logic.tempBoard[6][0]).toEqual("B");
});
});

this seems to work the way i want. how can i get it to work in the beforeEach() section?

Upvotes: 1

Views: 534

Answers (1)

Eitan Peer
Eitan Peer

Reputation: 4345

var logic should be defined in the scope of the describe function, then it exists both in the scope of the beforeEach function and the spec (the it function), e.g.

describe('suite', function () {
    var myVar;
    beforeEach(function(){
        myVar = 10;
    });
    it('checks myVar', function () {
        expect(myVar).toEqual(10);
    });
});

Upvotes: 3

Related Questions