简体   繁体   English

运行jasmine测试时,我怎么知道我是否在一个描述块中,在每个块之前还是阻塞?

[英]When running jasmine tests, how can I know if I am in a describe block, beforeEach block or it block?

I need to throw an exception if a utility is used outside of an 'it' or 'beforeEach' block in my tests. 如果在我的测试中在'it'或'beforeEach'块之外使用了一个实用程序,我需要抛出异常。 Example - 示例 -

   describe('some test', function(){

     useUtil();     // should throw exception

     beforeEach(function(){
        useUtil()   // should work
     })

     it('should test something', function(){
        useUtil()   // should work
     }) 
   })

The util creates spies, and I want to make sure they are created in a way that allows Jasmine to clean them after every suite. util创建了间谍,我想确保它们的创建方式允许Jasmine在每个套件之后清理它们。

You could create a globally accessible variable called isSpecPhase , and set it initially to false . 您可以创建一个名为isSpecPhase的全局可访问变量,并将其初始设置为false

Then, define a global beforeEach: 然后,定义一个全局的beforeEach:

beforeEach(function () {
    isSpecPhase = true;
});

Make sure to define the beforeEach before all your other suites, so that it runs before each of your specs. 确保在所有其他套件之前定义beforeEach ,以便它在每个规范之前运行。 In your util function, you could then check if isSpecPhase === true , and throw an exception otherwise. 在您的util函数中,您可以检查isSpecPhase === true是否isSpecPhase === true ,否则抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM