2010-03-31 21 views
10

tengo dos modelos:RSpec, stubbing métodos de recursos anidados

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

class User < ActiveRecord::Base 
    has_many :solutions 
end 

y anidan Me soluciones dentro de los usuarios de la siguiente manera:

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

y, finalmente, aquí está la acción I "m tratando a las especificaciones :?

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 

Mi pregunta es, ¿cómo diablos puedo Spec @user.solutions.find(params[:id])

Aquí es mi especificación actual:

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 

Pero eso me pone el siguiente error:

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

¿Alguien puede ayudarme con la forma en que puedo código auxiliar @user.solutions.new(params[:id])?

Respuesta

25

Parece que encontré mi propia respuesta, pero la voy a publicar aquí ya que no puedo encontrar mucho acerca de esto en la red.

RSpec tiene un método llamado stub_chain: http://apidock.com/rspec/Spec/Mocks/Methods/stub_chain

que hace que sea fácil de muñón de un método como:

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

al hacer esto:

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

Entonces puedo escribir una Prueba de RSpec como esta:

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 

Y mi especificación pasa. Sin embargo, todavía estoy aprendiendo aquí, así que si alguien piensa que mi solución aquí no es buena, no dude en comentarla y trato de hacerlo bien.

Joe

+0

muy útil, gracias. – zetetic

+0

¡Bienvenido, solo vote las respuestas, por favor! – TheDelChop

7

Con la nueva sintaxis RSpec, usted tropieza una cadena como así

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