简体   繁体   English

如何使用Rspec测试关联模型

[英]How do I test associated models with Rspec

In my app I have following models: 在我的应用程序中,我有以下模型:

class UserApplication < ActiveRecord::Base
  has_many :builds, dependent: :destroy
  belongs_to :user
end


class Build < ActiveRecord::Base
  belongs_to :user_application
end

And User model is generated by Devise . 用户模型由Devise生成。

My question is: is I want to test, say, model validations, should I do it like this: 我的问题是:我想测试模型验证,我应该这样做:

require 'spec_helper'

describe UserApplication do

  it "is invalid without name" do
    user_application = UserApplication.new(name: nil)
    expect(user_application).to have(1).errors_on(:name)
  end
end

Or should I create UserApplication through User ? 或者我应该通过用户创建UserApplication In general, should I bear in mind associations when testing my models, if test example is not connected to relationships? 一般来说,在测试我的模型时,我是否应该牢记关联,如果测试示例与关系无关?

You should test validations/associations/fields existence/etc in corresponding specs. 您应该在相应的规范中测试验证/关联/字段存在/等。 I don't see your UserApplication validations, but if I correctly understand you, your specs for this model should look like this(I am using shoulda and shoulda-matchers gems for testing): 我没有看到您的UserApplication验证,但如果我正确理解您,您对此模型的规范应如下所示(我使用的是shouldashoulda-matchers gems进行测试):

require 'spec_helper'

describe UserApplication do
  let!(:user_application) { create(:user_application) }

  it { should have_many(:builds).dependent(:destroy) }
  it { should belong_to(:user) }
  it { should validate_presence_of(:name) }
end

I am always creating only the instance of the model I want to test. 我总是只创建我想测试的模型的实例。 It is important to test that associations exist and correct, but you don't need to create testing model instance through association. 测试关联是否存在和正确非常重要,但您不需要通过关联创建测试模型实例。

It seems prudent to have test code parallel app code as closely as possible. 尽可能使测试代码并行应用程序代码似乎是谨慎的。 That is, if UserApplication will be created via User in the controller, it ought to be done the same way in the test. 也就是说,如果UserApplication将通过控制器中的User创建,则应该在测试中以相同的方式完成。 Furthermore, your UserApplication validations will probably test the association sooner or later anyway, so the test subject should be created in such a way as to be valid. 此外,您的UserApplication验证可能迟早会测试该关联,因此应以有效的方式创建测试主题。 With that in mind, you can set up your tests as follows: 考虑到这一点,您可以按如下方式设置测试:

require 'spec_helper'

describe UserApplication do
  let(:user) { User.create(user_params) }
  before { @user_application = user.user_applications.build(name: 'name') }

  subject { @user_application }

  describe 'validations' do
    context 'when name is missing' do
      before { @user_application.name = '' }
      it { should_not be_valid }
    end

    context 'when user_id is missing' do
      before { @user_application.user_id = nil }
      it { should_not be_valid }
    end

    # other validations
  end
end

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM