简体   繁体   English

如何使用Shoulda-matchers gem在rspec中测试关联?

[英]How to test associations in rspec using shoulda-matchers gem?

Hello I am learning to test rails application using rspec. 您好,我正在学习使用rspec测试Rails应用程序。 I am testing bank application where transaction belongs to account. 我正在测试交易属于帐户的银行应用程序。 I am testing the Transaction model. 我正在测试事务模型。 It's code is as follows: Transaction.rb: 它的代码如下: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

And it's spec is as follows: 它的规格如下:

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

I used shoulda gem for association. 我用shoda宝石来联想。 However when I run this test, it throws error as ' account must exist', even though I have added the association. 但是,当我运行此测试时,即使我添加了关联,它也会由于“帐户必须存在”而引发错误。

Error: 错误:

.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

Can anybody please help to understand what is it I am doing wrong? 有人可以帮忙了解我在做什么错吗?

PS: Transaction table has account_id column for the association. PS:交易表具有用于关联的account_id列。

Thanks inadvance. 提前致谢。

In Rails 5, associations defined as belongs_to are required by default. 在Rails 5中,默认情况下需要定义为belongs_to关联。 So when you are checking expect(subject).to be_valid , the valid? 因此,当您检查expect(subject).to be_validvalid? call is returning false because you don't have an account set. 呼叫返回false,因为您没有设置帐户。

Your approach to get your tests working will then depend on what you want your application's object model to look like. 然后,使测试工作的方法将取决于您希望应用程序的对象模型是什么样。

If a transaction can exist without an account, then set up your association that way: 如果可以在没有帐户的情况下进行交易,则可以通过以下方式建立关联:

class Transaction < ApplicationRecord
  belongs_to :account, optional: true
end

However, I think it's more likely that a transaction without an account doesn't make sense. 但是,我认为没有帐户的交易更有可能没有意义。 So your current test is actually right – when you're testing an accountless transaction, it's not valid. 所以,当前的测试确实是正确的-当你测试一个accountless交易,这是无效的。

In this case, you can set up your test subject accordingly: 在这种情况下,您可以相应地设置测试主题:

RSpec.describe Transaction, type: :model do
  let(:account) { Account.new }
  subject {
    described_class.new( ..., account: account)
  }
  ...
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 Rspec 测试 has_many? [Shoulda-matchers] - How to pass Rspec test has_many? [Shoulda-matchers] 使用/安装Shoulda-matchers gem时出现问题 - Trouble using/installing shoulda-matchers gem 使用rspec + Shoulda-matchers进行测试 - testing with rspec + shoulda-matchers 尝试使用shoulda-matcher / RSpec测试唯一性验证时出现“没想到......”错误 - Getting a “Did not expect…” error when trying to test uniqueness validation using shoulda-matchers/RSpec 使用Shoulda-Matchers和FactoryGirl测试STI关联 - Testing STI Associations with Shoulda-Matchers & FactoryGirl rails / rspec没有看到匹配器 - Shoulda-matchers are not seen by rails/rspec 使用`shoulda-callback-matchers` gem + rails 5 + rspec测试after_create_commit回调 - Test after_create_commit callback using `shoulda-callback-matchers` gem + rails 5 + rspec NoMethodError:未定义的方法`validate_presence_of'(Rspec和Shoulda-Matchers) - NoMethodError: undefined method `validate_presence_of' (Rspec and Shoulda-Matchers) NoMethodError:在单元测试中使用 shoulda-matchers 时,未定义的方法“belong_to”用于测试 - NoMethodError: undefined method `belong_to' for test when using shoulda-matchers in unit test Rails:应该-匹配器属于可选测试 - Rails: Shoulda-matchers belong_to optional test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM