我已经定义了以下功能
Feature: Shoper can add an item to ShoppingCart
Scenario: First item added to ShoppingCart
Given I have an Empty ShoppingCart
When I add an Item to ShoppingCart
Then The ShoppingCart must have 1 item
Scenario: Second item added to ShoppingCart
Given I already have 1 item on my ShoppingCart
When I add a second item to ShoppingCart
Then The ShoppingCart must have 2 items
并使用 CucumberJs 生成步骤定义,如下所示:
'use strict';
module.exports = function () {
this.Given(/^I have an Empty ShoppingCart$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.When(/^I add an Item to ShoppingCart$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.Then(/^The ShoppingCart must have (\d+) item$/, function (arg1, callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
}
但我没有找到一种方法来为我的可观察视图模型创建一个实例来在那里测试它
function ShopCartViewModel() {
var self = this;
self.items = ko.observableArray([]);
self.grandTotal = ko.computed(function() {
var total = 0;
ko.utils.arrayForEach(this.items(), function(item) {
total += item.price();
});
return total.toFixed(2);
}, this);
self.getItemFromList = function ( id ) {
return ko.utils.arrayFirst(self.items(), function (item)
{
if (item.ProductId() === id()) {
return item;
}
});
}
}
我试图加载淘汰赛并加载我的视图模型后:
var ko = require('../../js/knockout-3.1.0');
var ShopCart = require('../../js/shopCartViewModel');
var cart;
console.log('ko :', ko); // <-defined
console.log('cart :', ShopCart); // <- empty object {}
this.Given(/^I have an Empty ShoppingCart$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
cart = new ShopCart(); // <- error
callback.pending();
});
ko 工作,但 ShopCart 返回{}
如何为 ViewModel
内部 CucumberJs 步骤定义创建实例?