2014-10-07 3 views
1

그래서 작은 작업 추적 애플리케이션을 작성하여 레일 학습을 시작했습니다. 이제 리팩토링을 좀 더 안전하게하려고 노력 중입니다. 예를 들어 업데이트 또는 파기 전에 범위를 사용하여 자신의 작업을보고 있는지 확인합니다.사용자 범위 파인더로 전환 한 후 Rspec 테스트가 실패했습니다.

그러나이 작업을 수행하면 세 가지 테스트가 더 이상 진행되지 않으며 문제를 해결하는 방법을 모르겠습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 테스트를 변경해야합니까, 아니면 공장에서 무언가를하는 것을 잊었습니까?

감사합니다.

**[email protected]** 
def update 
    @task = current_user.tasks.where(id: params[:id]) 
    authorize @task 

    if @task.update_attributes(task_params) 
    else 
    flash[:error] = "There was an error updating the todo." 
    end 

    respond_with(@task) do |format| 
    format.html { redirect_to tasks_path } 
    end 
end 

def destroy 
    @task = current_user.tasks.where(id: params[:id]) 
    authorize @task 

    if @task.destroy 
    flash[:notice] = "Todo was deleted successfully." 
    else 
    flash[:error] = "There was an error deleting the todo." 
    end 

    respond_with(@task) do |format| 
    format.html { redirect_to tasks_path } 
    end 
end 

시험 1 및 2

**task_controller_spec** 
require 'rails_helper' 

describe TasksController do 

    include Devise::TestHelpers 

    before do 
    @user = create(:user) 
    sign_in @user 
    end 

describe '#update-completed' do 
    it "updates a task to be completed" do 
     task = create(:task, user: @user) 
     expect(task.completed).to eq(false) 

     patch :update, id: task.id, task:{completed: true} 
     task.reload 
     expect(task.completed).to eq(true) 
    end 
    end 

    describe '#destroy' do 
    it "deletes a task" do 
     task = create(:task, user: @user) 
     delete :destroy, id: task.id 
     expect(@user.tasks.count).to eq(0) 
    end 
    end 
end 

테스트 3

초래
**feature_spec** 
require 'rails_helper' 

feature "Task" do 

    include Warden::Test::Helpers 
    Warden.test_mode! 

    before do 
    @user = create(:user) 
    login_as(@user, :scope => :user) 
    end 

    feature "completes", js: true do 
    scenario "a task using a checkbox" do 
    task = create(:task, user: @user) 
     visit tasks_path 
     check("task[completed]") 

     expect(page).to have_content('Todo completed!') 
    end 
    end 

    after do 
    Warden.test_reset! 
    end 
end 

...

1) TasksController#update-completed updates a task to be completed 
Failure/Error: patch :update, id: task.id, task:{completed: true} 
NoMethodError: 
    undefined method `user' for # <ActiveRecord::AssociationRelation::ActiveRecord_AssociationRelation_Task:0xc1e4450> 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.9/lib/active_record/relation/delegation.rb:121:in `method_missing' 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.9/lib/active_record/relation/delegation.rb:68:in `method_missing' 
# ./app/policies/application_policy.rb:26:in `update?' 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/pundit-0.3.0/lib/pundit.rb:70:in `public_send' 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/pundit-0.3.0/lib/pundit.rb:70:in `authorize' 
# ./app/controllers/tasks_controller.rb:28:in `update' 

2) TasksController#destroy deletes a task 
Failure/Error: delete :destroy, id: task.id 
NoMethodError: 
    undefined method `user' for #<ActiveRecord::AssociationRelation::ActiveRecord_AssociationRelation_Task:0xc51965c> 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.9/lib/active_record/relation/delegation.rb:121:in `method_missing' 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.9/lib/active_record/relation/delegation.rb:68:in `method_missing' 
# ./app/policies/application_policy.rb:26:in `update?' 
# ./app/policies/application_policy.rb:34:in `destroy?' 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/pundit-0.3.0/lib/pundit.rb:70:in `public_send' 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/pundit-0.3.0/lib/pundit.rb:70:in `authorize' 
# ./app/controllers/tasks_controller.rb:42:in `destroy' 

3) Task completes a task with a checkbox 
Failure/Error: Unable to find matching line from backtrace 
NoMethodError: 
    undefined method `user' for #<ActiveRecord::AssociationRelation::ActiveRecord_AssociationRelation_Task:0xbe5fb04> 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.9/lib/active_record/relation/delegation.rb:121:in `method_missing' 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.9/lib/active_record/relation/delegation.rb:68:in `method_missing' 
# ./app/policies/application_policy.rb:26:in `update?' 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/pundit-0.3.0/lib/pundit.rb:70:in `public_send' 
# /home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/pundit-0.3.0/lib/pundit.rb:70:in `authorize' 
# ./app/controllers/tasks_controller.rb:28:in `update' 

작업 공장

,745,
**task factory** 
    FactoryGirl.define do 
    factory :task do 
     description "MyText" 
     user nil 
     completed false 
    end 
    end 

답변

관련 문제