【发布时间】:2018-11-13 10:36:17
【问题描述】:
您好,我正在学习使用 rspec 测试 Rails 应用程序。我正在测试交易属于帐户的银行应用程序。我正在测试交易模型。它的代码如下: Transaction.rb:
class Transaction < ApplicationRecord
validates :amount, presence: true, numericality: { only_integer: true,
greater_than: 0 }
after_create :update_balance
belongs_to :account
def update_balance
if transaction_type == 'debit'
account.current_balance -= amount
else
account.current_balance += amount
end
end
end
它的规格如下:
require 'rails_helper'
RSpec.describe Transaction, type: :model do
it { should belong_to(:account)}
subject {
described_class.new(amount: 60, transaction_type: 'credit',
id: 1,
created_at: DateTime.now, updated_at: DateTime.now,
account_id: 1)
}
it 'is valid with valid attributes' do
expect(subject).to be_valid
end
it 'is not valid without amount' do
subject.amount = nil
expect(subject).to_not be_valid
end
it 'is not valid without transaction type' do
subject.transaction_type = nil
expect(subject).to_not be_valid
end
it 'is not valid without created_at date' do
subject.created_at = nil
expect(subject).to_not be_valid
end
it 'is not valid without updated_at date' do
subject.updated_at = nil
expect(subject).to_not be_valid
end
it 'is not valid without transaction id' do
subject.id = nil
expect(subject).to_not be_valid
end
it 'is not valid without account id' do
subject.id = nil
expect(subject).to_not be_valid
end
end
我使用 shoulda gem 进行关联。但是,当我运行此测试时,它会引发错误,因为“帐户必须存在”,即使我已经添加了关联。
错误:
.F......
Failures:
1) Transaction is valid with valid attributes
Failure/Error: expect(subject).to be_valid
expected #<Transaction id: 1, transaction_type: "credit", amount: 0.6e2, created_at: "2018-11-13 10:33:13", updated_at: "2018-11-13 10:33:13", account_id: 1> to be valid, but got errors: Account must exist
# ./spec/models/transaction_spec.rb:12:in `block (2 levels) in <top (required)>'
Finished in 0.02937 seconds (files took 0.77127 seconds to load)
8 examples, 1 failure
任何人都可以帮助理解我做错了什么吗?
P.S:事务表有关联的 account_id 列。
谢谢你。
【问题讨论】:
标签: ruby-on-rails rspec associations shoulda