rspec的问题并期待改变

时间:2017-10-11 00:30:52

标签: ruby-on-rails rspec

我正在使用rspec来测试Ruby终端应用程序。我似乎无法想出测试哈希值的变化

我仍然试图总体上考虑测试,所以我很可能误解了测试方面的多个问题。

我正在尝试测试将费用添加到帐户实例但费用超出帐户限额时。帐户余额不会改变。

class Account
  def apply_charge(card_holder_name, charge)
    charge       = int_dollar_input(charge)
    account      = find_account_by_card_holder(card_holder_name) 
    temp_balance = account[:balance] + charge

    account[:balance] = account[:balance] + charge if temp_balance < account[:limit]
  end
end

describe "Adding a charge to an account" do
  context "GIVEN a charge amount that exceeds the account limit" do

    let(:account)     { subject.add_account("William", 5454545454545454,'$1000') }

    it "will do nothing" do
      expect(subject.apply_charge('William', '$1200')).
        to_not change {account[:balance]} 
    end
  end
end

帐户是一个哈希数组

account.inspect = [{:card_holder_name=>"William", :card_number=>5454545454545454, :limit=>1000, :balance=>0}]
  

1)帐户向帐户添加费用给予收费金额   超出帐户限制将无能为力        故障/错误:          期待(subject.apply_charge('William','$ 1200'))。            to_not更改{account [:balance]}

   expected `account[:balance]` not to have changed, but was not given a block
 # ./spec/account_spec.rb:46:in `block (4 levels) in <top (required)>'

1 个答案:

答案 0 :(得分:2)

预计需要处于阻止状态:expect { subject.apply_charge('William', '$1200') }.to.....