2010-03-31 4 views
10

J'ai deux modèles:RSpec, stubbing méthodes de ressources imbriquées

class Solution < ActiveRecord::Base 
    belongs_to :owner, :class_name => "User", :foreign_key => :user_id 
end 

class User < ActiveRecord::Base 
    has_many :solutions 
end 

et je solutions imbriquer les utilisateurs comme ceci:

ActionController::Routing::Routes.draw do |map| 
    map.resources :users, :has_many => :solutions 
end 

et enfin voici l'action que je "m essayer de spec :

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 

Ma question est, comment diable puis-je SPEC @user.solutions.find(params[:id])

Voici mon spec actuelle:

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 

Mais qui me fait l'erreur suivante:

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

Quelqu'un peut-il me aider avec comment je peux bouchonner @user.solutions.new(params[:id])?

Répondre

25

On dirait que j'ai trouvé ma propre réponse, mais je vais le poster ici car je n'arrive pas à trouver beaucoup de choses à ce sujet sur le net.

RSpec a une méthode appelée stub_chain: http://apidock.com/rspec/Spec/Mocks/Methods/stub_chain

qui le rend facile à stub une méthode comme:

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

en faisant ceci:

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

Alors je peux écrire un RSpec test comme ceci:

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 

Et mes spécifications passent. Cependant, je suis encore en train d'apprendre ici, donc si quelqu'un pense que ma solution n'est pas bonne, n'hésitez pas à la commenter et j'essayerai d'y arriver totalement.

Joe

+0

Très utile, merci. – zetetic

+0

Votre bienvenue, il suffit de voter pour les réponses s'il vous plaît! – TheDelChop

7

Avec la nouvelle syntaxe RSpec, vous Stub une chaîne comme si

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