什么是jasmine. describe是什么

本文介绍Jasmine行为驱动开发测试框架的基本用法。Jasmine不依赖任何其他JavaScript框架,也不需要DOM支持。本文通过示例讲解了关键API,如describe、it、expect等的使用方法。

Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.

上面是jasmine官方文档里对它的解释,下面用中文简单的翻译下

jasmine是一个行为驱动开发的测试框架,不依赖任何js框架以及dom,是一个非常干净以及友好API的测试库.
下面简单的以一个例子来说明它的用法

定义一个测试文件命令为test.js

describe("A spec (with setup and tear-down)", function() {
  var foo;

  beforeEach(function() {
    foo = 0;
    foo += 1;
  });

  afterEach(function() {
    foo = 0;
  });

  it("is just a function, so it can contain any code", function() {
    expect(foo).toEqual(1);
  });

  it("can have more than one expectation", function() {
    expect(foo).toEqual(1);
    expect(true).toEqual(true);
  });
});

上面的例子来自于官网,这里只说下几个重要的API,更多的用法请,点击这里

首先任何一个测试用例以describe函数来定义,它有两参数,第一个用来描述测试大体的中心内容,第二个参数是一个函数,里面写一些真实的测试代码

it是用来定义单个具体测试任务,也有两个参数,第一个用来描述测试内容,第二个参数是一个函数,里面存放一些测试方法

expect主要用来计算一个变量或者一个表达式的值,然后用来跟期望的值比较或者做一些其它的事件

beforeEach与afterEach主要是用来在执行测试任务之前和之后做一些事情,上面的例子就是在执行之前改变变量的值,然后在执行完成之后重置变量的值

最后要说的是,describe函数里的作用域跟普通JS一样都是可以在里面的子函数里访问的,就像上面的it访问foo变量

想要运行上面的测试例子可以通过karma 来运行,命令例子如下

karma start test/karma.conf.js#前端

这是我的模拟: beforeEach(waitForAsync(() => { sharedService = jasmine.createSpyObj('SharedService', ['getQueryParameter']); httpService = jasmine.createSpyObj('HttpService', ['getEnergySavingMeasurementData']); plxGlobalLoading = jasmine.createSpyObj('PlxGlobalLoadingService', ['open', 'close']); plxMessageService = jasmine.createSpyObj('PlxMessage', ['error']); plxChartsService = jasmine.createSpyObj('PlxChartsService', ['getInstanceByDom']); translateService = jasmine.createSpyObj('TranslateService', ['instant']); // 设置默认的mock返回值 sharedService.getQueryParameter.and.returnValue(new QueryParameter()); translateService.instant.and.callFake((key: string, params?: any) => { if (params) { return `${key}_translated`; } return key; }); TestBed.configureTestingModule({ declarations: [RootViewResultComponent, MockTranslatePipe], providers: [ { provide: Router, useValue: {} }, { provide: TranslateService, useValue: translateService }, { provide: PlxChartsService, useValue: plxChartsService }, { provide: PlxGlobalLoadingService, useValue: plxGlobalLoading }, { provide: PlxMessage, useValue: plxMessageService }, { provide: HttpService, useValue: httpService }, { provide: SharedService, useValue: sharedService }, { provide: LOCALE_ID, useValue: 'zh-CN' } ] }).compileComponents().then(() => { fixture = TestBed.createComponent(RootViewResultComponent); component = fixture.componentInstance; component.echartsInstance = { on: jasmine.createSpy('on').and.callThrough(), off: jasmine.createSpy('off').and.callThrough(), getZr: jasmine.createSpy('getZr').and.returnValue({ on: jasmine.createSpy('zrOn').and.callThrough(), off: jasmine.createSpy('zrOff') }), setOption: jasmine.createSpy('setOption'), dispatchAction: jasmine.createSpy('dispatchAction') }; }); }));
最新发布
07-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值