Jasmine - 何时使用 toContain() 或 toMatch()?
Jasmine - When to use toContain() or toMatch()?
我正在研究使用 Jasmine JS 进行的 TDD 和单元测试,我对它们的方法有疑问。
我找到了两种方法,想知道有什么区别。
describe('Teste do toContain', () => {
var name = 'Lucas de Brito Silva'
it('Deve demonstrar o uso do toContain', () => {
expect(name).toContain('Lucas');
});
});
describe('Teste do toMatch', function () {
var text = 'Lucas de Brito Silva'
it('deve validar o uso do toMatch', () => {
expect(text).toMatch('Brito');
});
})
不同之处部分在于它们在 上的操作,还有它们将要做什么。
下面是来自 version 2 of Jasmine 的示例用法(但它使用最新版本运行示例):
it("The 'toMatch' matcher is for regular expressions", function() {
var message = "foo bar baz";
expect(message).toMatch(/bar/);
expect(message).toMatch("bar");
expect(message).not.toMatch(/quux/);
});
describe("The 'toContain' matcher", function() {
it("works for finding an item in an Array", function() {
var a = ["foo", "bar", "baz"];
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
it("also works for finding a substring", function() {
var a = "foo bar baz";
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/boot.min.js"></script>
它确实展示了他们的能力。
toContain
will work on both arrays and strings. It will essentially be the same using Array#includes
or String#includes
- 如果数组或字符串具有 item(对于数组)或 子序列,将检查它(对于字符串)与参数匹配。 expect(something).toContain(other)
大致类似于检查 something.includes(other) === true
.
toMatch
而是使用正则表达式。所以,首先,它只适用于字符串,不适用于数组。其次,如果给定一个字符串作为参数 从中生成 一个正则表达式。所以,expect(something).toMatch(other)
实际上会像 new RegExp(other).test(something)
一样被解析。这确实意味着如果你想将它用于简单的字符串匹配,你应该注意不要使用特殊字符:
it("The 'toMatch' matcher generates a regex from the input", function() {
var message = "foo\dbar";
expect(message).toMatch(message);
});
it("The generated matcher will obey regex restrictions", function() {
var pattern = "foo\dbar";
expect(pattern).not.toMatch(pattern);
expect("foo4bar").toMatch(pattern);
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/boot.min.js"></script>
在这里,message
字符串的值是 foo\dbar
但是如果你从中生成一个正则表达式,那么它 不会 匹配相同的字符串,因为 \d
表示数字 - foo4bar
将匹配但不匹配 foo\dbar
.
我正在研究使用 Jasmine JS 进行的 TDD 和单元测试,我对它们的方法有疑问。
我找到了两种方法,想知道有什么区别。
describe('Teste do toContain', () => {
var name = 'Lucas de Brito Silva'
it('Deve demonstrar o uso do toContain', () => {
expect(name).toContain('Lucas');
});
});
describe('Teste do toMatch', function () {
var text = 'Lucas de Brito Silva'
it('deve validar o uso do toMatch', () => {
expect(text).toMatch('Brito');
});
})
不同之处部分在于它们在 上的操作,还有它们将要做什么。
下面是来自 version 2 of Jasmine 的示例用法(但它使用最新版本运行示例):
it("The 'toMatch' matcher is for regular expressions", function() {
var message = "foo bar baz";
expect(message).toMatch(/bar/);
expect(message).toMatch("bar");
expect(message).not.toMatch(/quux/);
});
describe("The 'toContain' matcher", function() {
it("works for finding an item in an Array", function() {
var a = ["foo", "bar", "baz"];
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
it("also works for finding a substring", function() {
var a = "foo bar baz";
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/boot.min.js"></script>
它确实展示了他们的能力。
toContain
will work on both arrays and strings. It will essentially be the same usingArray#includes
orString#includes
- 如果数组或字符串具有 item(对于数组)或 子序列,将检查它(对于字符串)与参数匹配。expect(something).toContain(other)
大致类似于检查something.includes(other) === true
.toMatch
而是使用正则表达式。所以,首先,它只适用于字符串,不适用于数组。其次,如果给定一个字符串作为参数 从中生成 一个正则表达式。所以,expect(something).toMatch(other)
实际上会像new RegExp(other).test(something)
一样被解析。这确实意味着如果你想将它用于简单的字符串匹配,你应该注意不要使用特殊字符:
it("The 'toMatch' matcher generates a regex from the input", function() {
var message = "foo\dbar";
expect(message).toMatch(message);
});
it("The generated matcher will obey regex restrictions", function() {
var pattern = "foo\dbar";
expect(pattern).not.toMatch(pattern);
expect("foo4bar").toMatch(pattern);
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/boot.min.js"></script>
在这里,message
字符串的值是 foo\dbar
但是如果你从中生成一个正则表达式,那么它 不会 匹配相同的字符串,因为 \d
表示数字 - foo4bar
将匹配但不匹配 foo\dbar
.