久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么嵌套的 describe() 塊看不到外部塊中定義的

Why can#39;t nested describe() blocks see vars defined in outer blocks?(為什么嵌套的 describe() 塊看不到外部塊中定義的變量?)
本文介紹了為什么嵌套的 describe() 塊看不到外部塊中定義的變量?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在實際代碼中遇到過這個問題,但我整理了一個簡單的例子來證明這一點.

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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 日韩小视频| 国产精品久久亚洲7777 | 天堂中文资源在线 | 99亚洲| 成人免费一区二区 | 亚洲精品一区在线 | 久久精品国产一区二区电影 | 久久久.com| 国产一区二区在线视频 | caoporn视频 | 国产色婷婷精品综合在线播放 | 日韩欧美精品 | 日韩最新网站 | 日本一区二区电影 | 国产露脸国语对白在线 | 欧美亚洲国产精品 | 一区二区国产在线观看 | 国产日韩精品在线 | 黑人精品欧美一区二区蜜桃 | 中日韩毛片| 成人精品鲁一区一区二区 | 中文在线亚洲 | 97国产一区二区精品久久呦 | 亚洲欧洲在线观看视频 | 精品久久久网站 | 天堂一区二区三区四区 | 日批免费观看 | 国产精品日本一区二区在线播放 | 中文字幕国产日韩 | 国产亚洲精品久久久久动 | 蜜臀av日日欢夜夜爽一区 | 亚州精品天堂中文字幕 | 国产一区二区视频在线 | 久久国产精彩视频 | 91日日 | 日韩精品一二三 | h片在线免费观看 | 99re6在线视频精品免费 | 久久在线 | 青青草视频免费观看 | 亚洲欧美日韩中文字幕一区二区三区 |