2010-03-31 17 views
10

stubbing ben iki model var :?RSpec, iç içe kaynak yöntemleri

class SolutionsController < ApplicationController 
    before_filter :load_user 

    def show 
    if(@user) 
     @solution = @user.solutions.find(params[:id]) 
    else 
     @solution = Solution.find(params[:id]) 
    end 
    end 

    private 

    def load_user 
    @user = User.find(params[:user_id]) unless params[:user_id].nil? 
    end 
end 

Benim sorum, halt ben Spec nasıl @user.solutions.find(params[:id])

İşte benim şimdiki Spec var:

describe SolutionsController do 

    before(:each) do 
    @user = Factory.create(:user) 
    @solution = Factory.create(:solution) 
    end 

    describe "GET Show," do 

    before(:each) do 
     Solution.stub!(:find).with(@solution.id.to_s).and_return(@solution) 
     User.stub!(:find).with(@user.id.to_s).and_return(@user) 
    end 

    context "when looking at a solution through a user's profile" do 

     it "should find the specified solution" do 
     Solution.should_receive(:find).with(@solution.id.to_s).and_return(@solution) 
     get :show, :user_id => @user.id, :id => @solution.id 
     end 
    end 
    end 

Ama bu bana aşağıdaki hata alır:

1)Spec::Mocks::MockExpectationError in 'SolutionsController GET Show, when looking at a solution through a user's profile should find the specified solution' 
<Solution(id: integer, title: string, created_at: datetime, updated_at: datetime, software_file_name: string, software_content_type: string, software_file_size: string, language: string, price: string, software_updated_at: datetime, description: text, user_id: integer) (class)> received :find with unexpected arguments 
    expected: ("6") 
    got: ("6", {:group=>nil, :having=>nil, :limit=>nil, :offset=>nil, :joins=>nil, :include=>nil, :select=>nil, :readonly=>nil, :conditions=>"\"solutions\".user_id = 34"}) 

anybody @user.solutions.new(params[:id]) saplama şekli de bana yardımcı olabilir?

cevap

25

Kendi cevabımı bulmuşum gibi görünüyor, ancak bu konuda yayınlayacağım çünkü bu konuda net bir şey bulamıyorum. http://apidock.com/rspec/Spec/Mocks/Methods/stub_chain

kolay gibi bir yöntem saplama için yapar: o zaman ben bir yazabilirsiniz Yani

@user.stub_chain(:solutions, :find).with(@solution.id.to_s).and_return(@solution) 

: bunu yaparak

@solution = @user.solutions.find(params[:id]) 

RSpec bir yöntemi denir stub_chain vardır Bu gibi RSpec testi:

it "should find the specified solution" do 
    @user.solutions.should_receive(:find).with(@solution.id.to_s).and_return(@solution) 
    get :show, :user_id => @user.id, :id => @solution.id 
end 

Ve benim özgeçmişim geçer. Bununla birlikte, ben hala burada öğreniyorum, bu yüzden herkesin benim çözümümün iyi olmadığını düşündüğünden, lütfen bunu yorumlamaktan çekinmeyin ve tamamen doğru olmaya çalışıyorum. Yeni RSpec sözdizimi ile Joe

+0

Çok yararlı, teşekkür gibi bir zincir saplama. – zetetic

+0

Hoşgeldiniz, sadece cevapları oylayın lütfen! – TheDelChop

7

, bunu

allow(@user).to receive_message_chain(:solutions, :find) 
# or 
allow_any_instance_of(User).to receive_message_chain(:solutions, :find)