在服务器端,我们使用 Rspec/Cucumber 进行 BDD 开发 (ruby),vowsjs (node.js)
是否有可用于 Web 浏览器的 BDD 框架(不包括 qUnit 或 YUI test,因为这些只适用于 TDD)?
在服务器端,我们使用 Rspec/Cucumber 进行 BDD 开发 (ruby),vowsjs (node.js)
是否有可用于 Web 浏览器的 BDD 框架(不包括 qUnit 或 YUI test,因为这些只适用于 TDD)?
看一下 jasmine
describe("Jasmine", function() {
it("makes testing JavaScript awesome!", function() {
expect(yourCode).toBeLotsBetter();
});
});
http://pivotal.github.com/jasmine/
https://github.com/pivotal/jasmine
对于熟悉 Ruby 的人来说,这应该很熟悉。
您还可以查看Yadda。它不像CucumberJS那样是一个独立的测试框架,而是使您能够从其他框架(如Mocha、Jasmine、CasperJS、Zombie、Qunit等)中使用类似Gherkin的语法。
我同意Jasmine的观点,也建议看看Jasmine-species
另外值得注意的是Kyuri——它是一个用于javascript的Gherkin(类似Cucumber的DSL)解析器,最初它针对Vows.js,但它也可以生成普通的javascript桩代码(然而,它现在仍然有些bug)。
Feature: Simple maths
In order to do maths
As a developer
I want to increment variables
Scenario: Increment variable once
Given a variable set to 1
When I increment the variable by 1
Then the variable should contain 2
步骤定义
var variable;
Given(/^a variable set to (\d+)$/, function(number, callback) {
variable = parseInt(number);
callback();
});
When(/^I increment the variable by (\d+)$/, function(number, callback) {
variable += parseInt(number);
callback();
});
Then(/^the variable should contain (\d+)$/, function(number, callback) {
if (variable != parseInt(number))
throw(new Error('Variable should contain '+number+' but it contains '+variable+'.'));
callback();
});
但没关系,这是一个很好的起点。我也不喜欢重新发明轮子(使用基于txt的语言)。我找到了一个建立在Jasmine之上的BDD框架,对我来说这是完美的解决方案:https://github.com/DealerDotCom/karma-jasmine-cucumber
例如:
specs.js(我们要做什么)
feature('Calculator: add')
.scenario('should be able to add 2 numbers together')
.when('I enter "1"')
.and('I add "2"')
.then('I should get "3"')
.scenario('should be able to add to a result of a previous addition')
.given('I added "1" and "2"')
.when('I add "3"')
.then('I should get "6"')
steps.js(我们如何做到的)
featureSteps('Calculator:')
.before(function(){
this.values = [];
this.total = null;
})
.given('I added "(.*)" and "(.*)"', function(first, second){
this.when('I enter "' + first + '"');
this.when('I add "' + second + '"');
})
.when('I enter "(.*)"', function(val){
this.values.push(val * 1);
})
.when('I add "(.*)"', function(val){
this.values.push(val * 1);
this.total = this.values[0] + this.values[1];
this.values = [this.total];
})
.then('I should get "(.*)"', function(val){
expect(this.total).toBe(val * 1);
})
更新 2016-02-16:
经过几个月的行为驱动开发实践,我最终以基于文本的特性描述和 Gherkin 为结果。我认为,将高度抽象的内容写入特性描述中要比之前在我的 karma-jasmine-cucumber 示例中写的更好。根据我的旧例子,现在我宁愿写出类似这样的内容:
Scenario: Addition of numbers
Given I have multiple numbers
When I add these numbers together
Then I should get their sum as result
这是我目前喜欢的方式。我曾经让步骤定义来设置夹具的值和断言,但当然,如果你愿意,你可以使用gherkin来提供Examples
:
Scenario: Addition of numbers
Given I have <multiple numbers>
When I add these numbers together
Then I should get <their sum> as result
Examples:
| multiple numbers | their sum |
| 1, 2, 3, 6 | 12 |
| 8, 5 | 13 |
| 5, -10, 32 | 27 |
Cucumber 将这3行转换为3个场景,例如:
Given I have 1, 2, 3, 6
When I add these numbers together
Then I should get 12 as result