Christian-health / StudyNote2017

2017年学习笔记
0 stars 0 forks source link

前端自动化测试学习笔记 #14

Open Christian-health opened 6 years ago

Christian-health commented 6 years ago

suite 测试集

tear(眼泪) down 拆除

specs 测试用例

前端测试-jasmine
原文地址: http://www.jianshu.com/p/cbfbfe65d777

笔记: toEqual,toBeTruthy()方法和toBeFalsy()方法详细解释:

https://www.safaribooksonline.com/library/view/javascript-testing-with/9781449356729/_yes_or_no_tobetruthy_tobefalsy.html

Yes or No? toBeTruthy, toBeFalsy
是或否? toBeTruthy,toBeFalsy
To test if something evaluates to true, you use the toBeTruthy matcher:
要测试某些评估结果为true,您可以使用toBeTruthy匹配器:

expect(true).toBeTruthy();
expect(12).toBeTruthy();
expect({}).toBeTruthy();
Likewise, to test if something evaluates to false, you use toBeFalsy:
同样,为了测试某些评估结果为false,您可以使用toBeFalsy;
expect(false).toBeFalsy();
expect(null).toBeFalsy();
expect("").toBeFalsy();
Note that Jasmine’s evaluation of truthy and falsy are identical to JavaScript’s.
请注意,Jasmine对truthy 和falsy的评估与JavaScript相同
This means that true is truthy, but so is "Hello world", or the number 12, or an object.
这意味着true 是 truthy,但是“Hello world”,或者数字12或对象也是如此( truthy)。
 It’s useful to think of all the things that are falsy, and then everything else as truthy.
【这句话咋翻译???????】
For reference, here’s a list of things that are falsy in Jasmine (and in JavaScript, too):
作为参考,以下是Jasmine中的一些falsy的列表(也可以在JavaScript中):
【也就是说,下面这些东西,在jasmine中都是假的】

1. false  
2. 0
3. ""
4. undefined (note that the variable undefined isn’t always undefined!)
5. null
6. NaN
7. 

If you haven’t seen NaN before, it’s a special number value that stands for Not a Number.
 It represents nonsensical number values like 0/0. It’s also returned by some functions that return numbers (for example, parseInt("hello") returns NaN because it cannot properly parse a number).
如果您以前没有看过NaN,那么它是一个特殊的数字值代表不是数字。 它代表无意义的数字值,如0/0。 它也返回一些返回数字的函数(例如,parseInt(“hello”)返回NaN,因为它无法正确解析数字)。
If you want to make sure something is literally true or false and nothing else, use the toEqual matcher like so:
如果你想确保一些事情真的是真的,没有别的,使用toEqual匹配器,就像这样:
expect(myVariable).toEqual(true);
expect(myOtherVariable).toEqual(false);

1

自定义matcher没看如何实现。

Christian-health commented 6 years ago

JavaScript单元测试框架-Jasmine 原文地址: http://www.cnblogs.com/zhcncn/p/4330112.html 2

Christian-health commented 6 years ago

http://oomusou.io/jasmine/jasmine-spy/#Spy#calls

Spy#calls

any(): boolean

若 Spy 被呼叫一次以上,則傳回 true,否則傳回 false。

it(`should use getTitle()`, () => {
  const spy = spyOn(component, 'getTitle');
  fixture.detectChanges();

  expect(spy.calls.any()).toBeTruthy();
});

測試 spy.calls.any(),若曾被呼叫過為 true,否則為 false。 這種寫法等效於:

it(`should use getTitle()`, () => {
  const spy = spyOn(component, 'getTitle');
  fixture.detectChanges();

  expect(spy).toHaveBeenCalled();
});

count(): boolean

傳回 Spy 被呼叫的次數。

it(`should use getTitle() for once`, () => {
  const spy = spyOn(component, 'getTitle');
  fixture.detectChanges();

  expect(spy.calls.count()).toBe(1);
});

測試 spy.calls.count(),可精確測試 Spy 被呼叫的次數。 這種寫法等效於:

it(`should use getTitle() for once`, () => {
  const spy = spyOn(component, 'getTitle');
  fixture.detectChanges();

  expect(spy).toHaveBeenCalledTimes(1);
});
Christian-health commented 6 years ago

jasmine函数库: 比如能查看到:

.calls.mostRecent(): returns the context (the this) and arguments for the most recent call
.calls.mostRecent():返回最近一次调用的上下文(this)和参数

https://jasmine.github.io/2.6/introduction.html

Christian-health commented 6 years ago

http://blog.csdn.net/cchenliang/article/details/72829165