我是一个在轨道上进行测试的菜鸟,并没有遵循TDD,因此我在事后进行了测试。我知道我的代码在我理智检查时有效,但我不能让这个简单的测试工作,我不知道为什么。我试图了解有关测试的更多信息,因此我可以成为更好的rails开发人员,并可以使用您的帮助。
我正在使用" show" "公司的行动"控制器作为仪表板。我做的一件事是在仪表板上显示所有付款(显示在控制器代码中)。我只是试图测试当你获得show动作时@payments被正确分配了。
这是我不断收到的失败信息:
1) CompaniesController GET show reacts to authenticated users sets @payments
Failure/Error: expect(assigns(:payments)).to match_array([payment1, payment2])
expected a collection that can be converted to an array with `#to_ary` or `#to_a`, but got nil
# ./spec/controllers/companies_controller_spec.rb:20:in `block (4 levels) in <top (required)>'
测试: (&#34;设置@ payments&#34;测试是唯一失败的)
require 'rails_helper'
describe CompaniesController do
describe "GET show" do
context "reacts to authenticated users" do
log_in_user
it "sets @company" do
company = Fabricate(:company)
get :show, id: company.id
expect(assigns(:company)).to eq(company)
end
it "sets @payments" do
company = Fabricate(:company)
payment1, payment2 = Fabricate(:payment, company: company), Fabricate(:payment, company: company)
get :show, id: company.id
expect(assigns(:payments)).to match_array([payment1, payment2])
end
end
end
end
控制器:
class CompaniesController < ApplicationController
before_action :authenticate_user!, if: :user_signed_in?
before_filter :correct_user, only: [:edit, :update, :show, :destroy]
def show
@user = current_user
@company = @user.company
@payments = @company.payments.order(id: :desc).page params[:page]
@payments_count = @company.payments.all
@refunds_count = @company.refunds.all
@refunded_amount = @refunds_count.sum(:amount)
@revenue = @company.payments.all.sum(:amount) - @refunded_amount
@refund = Refund.new
end
end
答案 0 :(得分:0)
@payments
来自您登录的用户的公司,因此您还需要确保log_in_user
人员current_user
与您想要获得的@payments
公司