简体   繁体   English

将数据从它阻塞的一个传递到另一个

[英]Pass data from one it block to another

在此处输入图像描述

As you can see in the above image, controller value assigned in first it block is not the same anymore in second it block.如上图所示,在第一个 it 块中分配的 controller 值在第二个 it 块中不再相同。 Is this the default behavior?这是默认行为吗? Can we do some changes in karma config file to get rid of this issue?我们可以在 karma 配置文件中做一些改变来摆脱这个问题吗?

This project is based on angularjs 1.7.9 which is using karma and jasmine.该项目基于使用业力的 angularjs 1.7.9 和 jasmine。

I think you can take advantage of beforeEach .我认为您可以利用beforeEach

describe('Login', function () {
  beforeEach(() => {
    $controller.name = 'Pankaj';
  });

  it('test one', function () {
    console.log($controller.name);
  });
   
  it('test two', function () {
    console.log($controller.name);
  });
});

beforeEach runs before every it block. beforeEach在每个it阻塞之前运行。

Edit编辑

You have to use describe, beforeEach, beforeAll, afterEach, and afterAll appropriately.您必须适当地使用describe, beforeEach, beforeAll, afterEach, and afterAll describe you can split by features/situations, beforeEach runs before every it , beforeAll runs once in a describe block, afterEach runs after each it in a describe block, and afterAll runs once after all the it blocks completed in a desribe block. describe您可以按功能/情况进行拆分, beforeEach在每个it之前运行, beforeAlldescribe afterEach describe每个it之后运行, desribe在所有it块在afterAll块中完成后运行一次。

describe('Login', function () {
  beforeEach(() => {
    $controller.name = 'Pankaj';
  });

  it('test one', function () {
    console.log($controller.name);
  });

  it('test two', function () {
    console.log($controller.name);
  });
  describe('dynamic part', function () {
    beforeEach(() => {
      // call the function that will change the variable
    });
    it('test 1', function () {
      // your assertions
    });
    it('test 2', function () {
      // your assertions
    });
  });
});

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

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