small medium large xlarge

31 Jan 2016, 16:28
Jon Parker (1 post)

Hi all,

I am trying the Cash withdrawal exercise in ‘The Cucumber Book’ - with the basic web page hook up.

I have a file C:\Ruby2\features\support\world_extentions with:

require ‘sinatra’

class UserInterface include Capybara::DSL

def withdraw_from(account, amount) Sinatra::Application.account = account visit ‘/’ fill_in ‘Amount’, :with => amount click_button ‘Withdraw’ end end

CAPTURE_CASH_AMOUNT = Transform /^$(\d+)$/ do |digits| digits.to_i end class CashSlot Sinatra::Application.cash_slot def initialize(cash_slot) @cash_slot = cash_slot end def contents @contents or raise(“I’m empty!”) end def dispense(amount) @contents = amount end end

class Account def deposit(amount) @balance = amount end def balance @balance end def debit(amount) @balance -= amount end def credit(amount) @balance =+ amount end end

class Teller def withdraw_from(account, amount) account.debit(amount) @cash_slot.dispense(amount) end def make_a_deposit(account, amount) end end

module KnowsTheUserInterface class UserInterface def withdraw_from(account, amount) end end def my_account @my_account ||= end def cash_slot Sinatra::Application.cash_slot end def teller @teller ||= end end World(KnowsTheUserInterface)

And then C:\Ruby2\lib\nice_bank with # encoding: utf-8 require ‘sinatra’

get ‘/’ do ‘Welcome to our Jons bank.’ end cash_slot = #set :cash_slot,

set :account do fail ‘account has not been set’ end

post ‘/withdraw’ do teller = teller.withdraw_from(settings.account, params[:amount].to_i) end

get ‘/’ do %{

Amount Withdraw

} end

set :account do fail ‘account has not been set’ end —- and when I run: bundle exec cucumber features\cashwithdrawal.feature -r features —- I get error: uninitialized constant CashSlot (NameError) C:/Ruby2/lib/nice_bank.rb:7:in <top (required)>' C:/Ruby2/features/support/env.rb:1:in require’ C:/Ruby2/features/support/env.rb:1:in <top (required)>' C:/Ruby2/lib/ruby/gems/2.0.0/gems/cucumber-2.3.2/lib/cucumber/rb_support/rb_language.rb:96:in load’ C:/Ruby2/lib/ruby/gems/2.0.0/gems/cucumber-2.3.2/lib/cucumber/rb_support/rb_language.rb:96:in `load_code_file’ etc.. —– I have spent hours looking up forums etc and can’t figure what is wrong - can anyone help please.

Many thanks

You must be logged in to comment