2010-03-31 19 views
10

छोटा करते मैं दो मॉडल मिल गया है :RSpec, नेस्टेड संसाधन तरीकों

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 

मेरे सवाल है, कैसे हो मैं कल्पना करते @user.solutions.find(params[:id])

यहाँ मेरे वर्तमान कल्पना है:

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 

लेकिन है कि मुझे निम्न त्रुटि हो जाता है:

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"}) 

किसी को भी मुझे मैं @user.solutions.new(params[:id]) कैसे ठूंठ कर सकते हैं के साथ मदद कर सकते हैं?

उत्तर

25

ऐसा लगता है कि मुझे अपना जवाब मिला, लेकिन मैं इसे यहां पोस्ट करने जा रहा हूं क्योंकि मुझे नेट पर इस बारे में बहुत कुछ नहीं मिल रहा है। http://apidock.com/rspec/Spec/Mocks/Methods/stub_chain

जो यह आसान की तरह एक विधि ठूंठ बना देता है: ऐसा करके

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

:

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

तो फिर मैं एक लिख सकते हैं

RSpec एक विधि कहा जाता stub_chain है इस प्रकार आरएसपीसी परीक्षण:

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 

और मेरा स्पेस पास हो गया। हालांकि, मैं अभी भी यहां सीख रहा हूं, इसलिए अगर कोई मेरा समाधान सोचता है तो कोई अच्छा नहीं है, तो कृपया इस पर टिप्पणी करने में संकोच न करें और मैं इसे पूरी तरह से सही करने की कोशिश करता हूं।

जो

+0

बहुत उपयोगी है, धन्यवाद की तरह एक श्रृंखला ठूंठ। – zetetic

+0

आपका स्वागत है, बस जवाबों को वोट दें! – TheDelChop

7
नई RSpec वाक्य रचना के साथ

, तो आप ऐसा

allow(@user).to receive_message_chain(:solutions, :find) 
# or 
allow_any_instance_of(User).to receive_message_chain(:solutions, :find) 
संबंधित मुद्दे