【问题标题】:Using Jasmine beforeEach asynchronously while passing this.result to test在传递 this.result 进行测试时异步使用 Jasmine beforeEach
【发布时间】:2020-05-26 04:02:10
【问题描述】:

以下代码未通过单元测试,因为 this.result 仍未定义。我的问题是变量范围、异步行为还是我没有想到的其他问题?我的最终目标是通过this.result == "somevalue"

的测试
describe("This is a test", ()=> {
  beforeEach(async ()=> {
    this.result = await "somevalue";
  })

  it("should be somevalue", async ()=> {
    await this.result;
    expect(this.result).toBe("somevalue")
  })
});

编辑:我尝试了以下方法并让它工作,但是有没有办法传递这个。[一些变量]正如我们在 Jasmine 文档中看到的使用beforeEach()?

describe("This is a test", ()=> {
  let result = undefined;
  beforeEach(async ()=> {
    result = await "somevalue";
  });

  it("should be somevalue", async ()=> {
    await result;
    expect(result).toBe("somevalue");
  })
});

【问题讨论】:

  • 你的单元测试中this的上下文是什么?组件?

标签: typescript promise async-await jasmine jasmine-node


【解决方案1】:

这是 Angular 吗?

尝试使用回调(完成)函数:

describe("This is a test", ()=> {
  beforeEach(async (done)=> {
    this.result = await "somevalue";
    done();
  })

  it("should be somevalue", async ()=> {
    await this.result;
    expect(this.result).toBe("somevalue")
  })
});

我觉得这很奇怪,因为我不知道this 的值是什么。

为什么我们awaiting "somevalue",它是一个字符串,没有任何异步的,但希望done 函数可以帮助你。

【讨论】:

  • 所以 Jasmine 文档中的 'this' 值实际上是一个变量,可以在 before 和 after 函数之间传递,并且仍然在 it() 方法中使用。根据名为“this 关键字”here 的 h3 标头
猜你喜欢
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 2019-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多