如何最好地测试has_many关联的控制器变量赋值?

时间:2017-06-30 15:19:18

标签: ruby-on-rails testing rspec

我正在尝试并且未能测试控制器以进行belongs_to对象的变量赋值。这些是控制器测试,我可以通过一些帮助真正体会到很多领域,即

  1. 我应该在这里以这种方式写这样的测试。
  2. 如果是这样,我怎么能让它发挥作用。
  3. 代码如下:

    Company.rb

    class Company < ApplicationRecord
        belongs_to  :user
        has_many        :employees, inverse_of: :company
        has_many        :quotes, inverse_of: :company
    end
    

    Quote.rb

    class Quote < ApplicationRecord
        belongs_to :company
    end
    

    Employee.rb

    class Employee < ApplicationRecord
        belongs_to :company
    end
    

    公司有一个普通CRUD的控制器,Quote有一个带有Show和Index的控制器,Employee没有控制器。公司#create创建所有三个对象,并{Quote#show显示redirect_to来自三个模型的各种颜色。

    companies_controller.rb #create

    def create
        @company = current_user.companies.new(company_params)
        if @company.save
          @quote = @company.quotes.last
          @employees = @company.employees.all
          redirect_to company_quote_url(@company, @quote, @employees), notice: 'Quote request created'
        else
          render :new
        end
    end
    

    quotess_controller.rb #show

    def show
        @company = Company.find(params[:company_id])
        @quote = @company.quotes.find(params[:id])
        @employees = @company.employees.all
    end
    

    我有一个工厂女孩工厂为模型的eahc设置:

    Companies.rb

    FactoryGirl.define do
      factory :company do
        sequence(:co_name) { |n| "Acme Co #{n}" }
        co_number "06488522"
        postcode "al1 1aa"
        industry :financial_services
    
        factory :company2 do
        end
    
        factory :company3 do
        end
      end
    end
    

    Quotes.rb

    FactoryGirl.define do
      factory :quote do
        lives_overseas true
        payment_frequency :monthly
    
        factory :quote2 do
        end
    
        factory :quote3 do
        end
      end
    end 
    

    Employees.rb

    FactoryGirl.define do
      factory :employee1, class: Employee do
        first_name "MyString"
        last_name "MyString"
        email "test@test.com"
        gender "MyString"
        date_of_birth "2000-06-20"
        salary 10000
    
        factory :employee2 do
        end
    
        factory :employee3 do
        end
      end
    end
    

    我正在尝试为Quote#show编写控制器测试并测试这三个对象的分配,即; @company@quote&amp; @employees到相对变量。代码到目前为止:

    quotes_controller_spec.rb

    require 'rails_helper'
    
    RSpec.describe QuotesController, type: :controller do
      let(:user) {FactoryGirl.create(:user) }
      let(:company) { FactoryGirl.create(:company, user: user) }
      let(:employee1) { FactoryGirl.create(:employee1, company: company) }
      let(:employee2) { FactoryGirl.create(:employee2, company: company) }
      let(:employee3) { FactoryGirl.create(:employee3, company: company) }
      let(:quote) { FactoryGirl.create(:quote, company: company) }
    
      describe "GET #show" do
        it "returns http success" do
          get :show, params: { company_id: company.id, id: quote.id, , employee_id: employee1.id }
          expect(response).to have_http_status(:success)
        end
    
        it "assigns requested quote to @quote" do
            get :show, params: { company_id: company.id, id: quote.id, employee1.id: employee1.id } #, employee_id: employee1.id
            expect(assigns(:quote)).to eq(quote) # passes fine
            expect(assigns(:company)).to eq(company) # passes fine
            expect(assigns(:employee1)).to eq(employee1) # fails 
        end
      end
    end
    

    我收到如下错误:

    故障:

      1) QuotesController GET #show assigns requested quote to @quote
         Failure/Error: expect(assigns(:employee1)).to eq(employee1)
    
           expected: #<Employee id: 1, first_name: "MyString", last_name: "MyString", email: "test@test.com", gender: "m",...alary: 10000, company_id: 178, created_at: "2017-07-01 11:21:27", updated_at: "2017-07-01 11:21:27">
                got: nil
    
           (compared using ==)
         # ./spec/controllers/quotes_controller_spec.rb:28:in `block (3 levels) in <top (required)>'
    

    当我运行应用程序并在公司#create之后在Quote#show template中使用params.inspect时,这些是传递的参数:

    <ActionController::Parameters {"controller"=>"quotes", "action"=>"show", "company_id"=>"109", "id"=>"109", "format"=>"#<Employee::ActiveRecord_AssociationRelation:0x007fc2694a07f8>"} permitted: false>
    

    我觉得我在这里有一些核心问题;

    1. 我需要以某种方式宣布Factory Girl

    2. 中的关联
    3. 我的测试应该以某种方式测试集合的存在及其对@employeesQuotes#show变量的分配,而不是仅仅分配一个员工记录,这就是我所做的。我正在尝试,而且没有做到。

    4. 我不确定我是否正在跨越分离线。也许应该存在,因为我正在测试Companies#create中创建并在Quotes#show中呈现的其他模型对象(公司,报价和员工)。

    5. 任何帮助和/或指导表示赞赏。下午阅读和谷歌搜索让我仍然不知道如何在这里得到我的测试策略和语法正确,以使其正常工作。顺便说一下,应用程序中的所有工作都很好,我只是想在此引号#show方法中测试正确对象的分配。感谢。

1 个答案:

答案 0 :(得分:0)

这个问题的第2部分答案,重新。在我的代码的上下文中测试集合的分配是:

expect(assigns(:employees)).to include(employee1)

expect(assigns(:employees)).to eq([employee1])

仍然在寻求对这个问题的第1部分和第3部分的反馈。

由于