J'ai deux modèles:Nested test des ressources RSpec
class Solution < ActiveRecord::Base
belongs_to :owner, :class_name => "User", :foreign_key => :user_id
end
class User < ActiveRecord::Base
has_many :solutions
end
avec le routage suivant:
map.resources :users, :has_many => :solutions
et voici le SOLUTIONSInterfaces:
class SolutionsController < ApplicationController
before_filter :load_user
def index
@solutions = @user.solutions
end
private
def load_user
@user = User.find(params[:user_id]) unless params[:user_id].nil?
end
end
Quelqu'un peut-il me aider à écrire un test pour l'action d'index? Jusqu'à présent, je l'ai essayé ce qui suit, mais il ne fonctionne pas:
describe SolutionsController do
before(:each) do
@user = Factory.create(:user)
@solutions = 7.times{Factory.build(:solution, :owner => @user)}
@user.stub!(:solutions).and_return(@solutions)
end
it "should find all of the solutions owned by a user" do
@user.should_receive(:solutions)
get :index, :user_id => @user.id
end
end
et je reçois l'erreur suivante:
Spec::Mocks::MockExpectationError in 'SolutionsController GET index, when the user owns the software he is viewing should find all of the solutions owned by a user'
#<User:0x000000041c53e0> expected :solutions with (any args) once, but received it 0 times
Merci d'avance pour toute l'aide.
Joe
EDIT:
Merci pour la réponse, je l'ai acceptée car elle a obtenu mon tellement plus, sauf que je reçois une autre erreur, et je ne peux pas comprendre tout à fait ce que son essayer de dites-moi:
Une fois que je crée les solutions au lieu de les construire, et ajouter le talon du User.find, je vois l'erreur suivante:
NoMethodError in 'SolutionsController GET index, when the user owns the software he is viewing should find all of the solutions owned by a user'
undefined method `find' for #<Class:0x000000027e3668>