require ‘rbehave’
require ’spec’ # for “should” method</p>
require ‘account’ # the actual application code
Story “transfer to cash account”,
%(As a savings account holder
I want to transfer money from my savings account
So that I can get cash easily from an ATM) do
Scenario “savings account is in credit” do
Given “my savings account balance is”, 100 do |balance|
@savings_account = Account.new(balance)
end
Given “my cash account balance is”, 10 do |balance|
@cash_account = Account.new(balance)
end
When “I transfer”, 20 do |amount|
@savings_account.transfer_to(@cash_account, amount)
end
Then “my savings account balance should be”, 80 do |expected_amount|
@savings_account.balance.should == expected_amount
end
Then “my cash account balance should be”, 30 do |expected_amount|
@cash_account.balance.should == expected_amount
end
end
Scenario “savings account is overdrawn” do
Given “my savings account balance is”, -20
Given “my cash account balance is”, 10
When “I transfer”, 20
Then “my savings account balance should be”, -20
Then “my cash account balance should be”, 10
end
end</div> </div> </div> </div> </div>
So I needed a way to capture the story concept using NUnit. I figured why not use the class text fixture attribute.
NUnit Syntax |
public class Transfer_to_cash_account : NBehaveAbstractFixture { /* As a savings account holder I want to transfer money from my savings account So that I can get cash easily from an ATM) */ |
RBehave Syntax |
Story “transfer to cash account”, %(As a savings account holder I want to transfer money from my savings account So that I can get cash easily from an ATM) do |
As you can see the story “transfer to cash account” is the name of the public class Transfer_to_cash_account. As I am sure you all noticed the [TestFixture] attribute is missing but you will also notice that the Transfer_to_cash_account class inherits from NBehaveAbstractFixture, more on this later. Lets talk about the real meat here, the “Given” “When” “Then” constructs.
NUnit Syntax |
[Test] public void savings_account_is_in_credit() { Account savings = null; Account cash = null; Given("my savings account balance is", 100, delegate(int accountBallance) { savings = new Account(accountBallance); }); Given("my cash account balance is", 10, delegate(int accountBallance) { cash = new Account(accountBallance); }); When("I transfer", 20, delegate(int transferAmount) { savings.TransferTo(cash, transferAmount); }); Then("my savings account balance should be", 100, delegate(int expectedBallance) { Assert.AreEqual(expectedBallance, savings.Ballance); }); } |
RBehave Syntax |
Scenario “savings account is in credit” do Given “my savings account balance is”, 100 do |balance| @savings_account = Account.new(balance) end Given “my cash account balance is”, 10 do |balance| @cash_account = Account.new(balance) end When “I transfer”, 20 do |amount| @savings_account.transfer_to(@cash_account, amount) end Then “my savings account balance should be”, 80 do |expected_amount| @savings_account.balance.should == expected_amount end Then “my cash account balance should be”, 30 do |expected_amount| @cash_account.balance.should == expected_amount end end |