問題描述
我在實際代碼中遇到過這個問題,但我整理了一個簡單的例子來證明這一點.
I've run into this issue in real code, but I put together a trivial example to prove the point.
以下代碼可以正常工作.我在我的根 describe()
塊中設置了一個變量,該變量可在我的 sub-describe()
s' it()
中訪問塊.
The below code works fine. I've set up a variable in my root describe()
block that is accessible within my sub-describe()
s' it()
blocks.
describe('simple object', function () {
var orchard;
beforeEach(function () {
orchard = {
trees: {
apple: 10,
orange : 20
},
bushes: {
boysenberry : 40,
blueberry: 35
}
};
});
describe('trees', function () {
it ('should have apples and oranges', function() {
var trees = orchard.trees;
expect (trees.apple).toBeDefined();
expect (trees.orange).toBeDefined();
expect (trees.apple).toEqual(10);
expect (trees.orange).toEqual(20);
});
it ('should NOT have pears or cherries', function() {
var trees = orchard.trees;
expect (trees.pear).toBeUndefined();
expect (trees.cherry).toBeUndefined();
});
});
});
http://jsfiddle.net/w5bzrkh9/
但是,如果我嘗試通過執行以下操作來稍微干掉我的代碼,它就會中斷:
However, if I try to DRY up my code a little by doing the following, it breaks:
describe('simple object', function () {
var orchard;
beforeEach(function () {
orchard = {
trees: {
apple: 10,
orange : 20
},
bushes: {
boysenberry : 40,
blueberry: 35
}
};
});
describe('trees', function () {
var trees = orchard.trees; // TypeError: Cannot read property 'trees' of undefined
it ('should have apples and oranges', function() {
expect (trees.apple).toBeDefined();
expect (trees.orange).toBeDefined();
expect (trees.apple).toEqual(10);
expect (trees.orange).toEqual(20);
});
it ('should NOT have pears or cherries', function() {
expect (trees.pear).toBeUndefined();
expect (trees.cherry).toBeUndefined();
});
});
});
http://jsfiddle.net/goqcev42/
在嵌套的 describe()
范圍內,orchard
對象是未定義的,即使它是在其中的 it()
塊中定義的.
Within the nested describe()
scope, the orchard
object is undefined, even though it's defined within the it()
blocks within it.
這是否是 Jasmine 的開發人員有意為之,可能是為了避免在 beforeEach()
中重置對象的問題以及可能破壞一些引用?他們如何做到這一點?我可以看到這可能很有用,我只是很好奇它是如何工作的.(我的猜測是一些 apply()
或 call()
魔術,但我不確定如何......)
Is this intentional on the part of Jasmine's developers, possibly to avoid issues with resetting the object in beforeEach()
and possible breaking some references? How do they make it happen? I could see how this might be useful, I'm just very curious as to how it works. (My guess is some apply()
or call()
magic, but I'm not sure how...)
--
作為旁注,我仍然可以通過簡單地使用另一個 beforeEach()
塊來干掉我的代碼:
As a side-note, I can still DRY up my code by simply using another beforeEach()
block:
describe('simple object', function () {
var orchard;
beforeEach(function () {
orchard = {
trees: {
apple: 10,
orange : 20
},
bushes: {
boysenberry : 40,
blueberry: 35
}
};
});
describe('trees', function () {
var trees;
beforeEach(function() {
trees = orchard.trees;
});
it ('should have apples and oranges', function() {
expect (trees.apple).toBeDefined();
expect (trees.orange).toBeDefined();
expect (trees.apple).toEqual(10);
expect (trees.orange).toEqual(20);
});
it ('should NOT have pears or cherries', function() {
expect (trees.pear).toBeUndefined();
expect (trees.cherry).toBeUndefined();
});
});
});
推薦答案
describe
塊的主體在 beforeEach
塊之前執行.
這完全符合預期.問題是您的 var trees
變量在初始化之前嘗試訪問 orchard
.describe
塊的主體在 beforeEach
塊之前執行.要解決這個問題,第三個代碼片段是唯一的方法.
The body of a describe
block is executed before the beforeEach
blocks.
This is exactly as expected. The problem is that your var trees
variable is trying to access orchard
before it has been initialized. The body of a describe
block is executed before the beforeEach
blocks. To solve this problem, the third code snippet is the only way to go.
Jasmine 將首先執行 describe 塊,然后在運行每個測試之前執行 beforeEach 塊.
Jasmine will first execute the describe blocks, and then execute the beforeEach blocks before running each test.
這篇關于為什么嵌套的 describe() 塊看不到外部塊中定義的變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!