我正在尝试并且未能测试控制器以进行belongs_to对象的变量赋值。这些是控制器测试,我可以通过一些帮助真正体会到很多领域,即
代码如下:
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>
我觉得我在这里有一些核心问题;
我需要以某种方式宣布Factory Girl
我的测试应该以某种方式测试集合的存在及其对@employees
中Quotes#show
变量的分配,而不是仅仅分配一个员工记录,这就是我所做的。我正在尝试,而且没有做到。
我不确定我是否正在跨越分离线。也许应该存在,因为我正在测试Companies#create
中创建并在Quotes#show
中呈现的其他模型对象(公司,报价和员工)。
任何帮助和/或指导表示赞赏。下午阅读和谷歌搜索让我仍然不知道如何在这里得到我的测试策略和语法正确,以使其正常工作。顺便说一下,应用程序中的所有工作都很好,我只是想在此引号#show方法中测试正确对象的分配。感谢。
答案 0 :(得分:0)
这个问题的第2部分答案,重新。在我的代码的上下文中测试集合的分配是:
expect(assigns(:employees)).to include(employee1)
或
expect(assigns(:employees)).to eq([employee1])
仍然在寻求对这个问题的第1部分和第3部分的反馈。
由于