我有两个型号:嵌套资源测试RSpec的
class Solution < ActiveRecord::Base
belongs_to :owner, :class_name => "User", :foreign_key => :user_id
end
class User < ActiveRecord::Base
has_many :solutions
end
与以下路由:
map.resources :users, :has_many => :solutions
这里是SolutionsController:
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
任何人可以帮助我写作索引行动的测试?到目前为止,我曾尝试以下,但它不工作:
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
而且我得到以下错误:
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
预先感谢所有帮助。
乔
编辑:
感谢您的回答,我接受它,因为它得到了我这么多的更远,除了我得到另一个错误,而我不能完全弄清楚它的努力告诉我:
一旦我创建了解决方案,而不是建立他们,我添加了User.find的存根,我看到以下错误:
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>