gor*_*vij 8 jasmine typescript karma-jasmine
根据mocha文档,不鼓励使用箭头函数.
https://mochajs.org/#arrow-functions
这对Jasmine来说是一样的吗?我在Jasmine文档中找不到关于该主题的任何指针.
Rad*_*ler 14
有一篇非常有趣的文章你不应该错过:
这是一个引用:
新的,更好的方式
对于每个测试(及其beforeEach/afterEach挂钩),jasmine将每个函数的接收器设置为最初为空的对象.这个对象在Jasmine的源代码中称为userContext,可以为其分配属性,并在每次测试结束时被吹走.为了解决我们遇到的问题,我们最近切换到为此对象分配变量,而不是在describe中声明它们然后分配它们.所以我们上面的原始代码现在看起来像这样:
Run Code Online (Sandbox Code Playgroud)describe('views.Card', function() { 'use strict'; beforeEach(function() { this.model = {}; this.view = new CardView(this.model); }); describe('.render', function() { beforeEach(function() { this.model.title = 'An Article'; this.view.render(); }); it('creates a "cardTitle" h3 element set to the model\'s title', function() { expect(this.view.$el.find('.cardTitle')).toContainText(this.model.title); });
那么,这一切意味着什么呢?我们应该使用茉莉花的箭头功能吗?
答案应该是 - 在代码中保留箭头函数,除了这个组合
// could be arrow
describe("ListModel -", () =>
{
// local context description
interface IMyTestContext
{
items?: Heroe[];
...
}
// could be arrow
describe("Test items ", () =>
{
// NOT AN ARROW - profit from Jasmine context passed as 'this'
beforeEach(function()
{
var ctx: IMyTestContext = this.TestContext = {};
// TODO do some defaults with context
...
});
// NOT AN ARROW - profit from Jasmine context passed as 'this'
it("should ...", function()
{
var ctx: IMyTestContext = this.TestContext;
// TODO ... test expecations
...
Run Code Online (Sandbox Code Playgroud)
所以,beforeEach()
和不要使用方向-从由代表茉莉花上下文中获利it()
this
我们还可以引入全球通话 beforeEach
import * as something from "...";
beforeEach(function()
{
this.TestContext = {};
});
Run Code Online (Sandbox Code Playgroud)
现在上下文总是存在,所以我们不必重新创建它:
describe("Track Changed items ", () =>
{
// NOT AN ARROW - profit from Jasmine context passed as 'this'
beforeEach(function()
{ // created by global beforeEach above
var ctx: IMyTestContext = this.TestContext;// = {};
Run Code Online (Sandbox Code Playgroud)
是的,这真是太神奇了,如果一个测试
beforeEach
运行器会找到一些全局......它也会在每次测试之前运行它......太棒了,不是吗?
归档时间: |
|
查看次数: |
3284 次 |
最近记录: |