使用关联测试 Rspec 控制器
我有两个模型:
class Solution < ActiveRecord::Base
belongs_to :user
validates_attachment_presence :software
validates_presence_of :price, :language, :title
validates_uniqueness_of :software_file_name, :scope => :user_id
has_attached_file :software
end
class User < ActiveRecord::Base
acts_as_authentic
validates_presence_of :first_name, :last_name, :primary_phone_number
validates_uniqueness_of :primary_phone_number
has_many :solutions
end
我的路线如下所示:
map.resources :user, :has_many => :solutions
现在我正在尝试使用以下 RSpec 测试来测试我的解决方案控制器:
describe SolutionsController do
before(:each) do
@user = Factory.build(:user)
@solution = Factory.build(:solution, :user => @user)
end
describe "GET index" do
it "should find all of the solutions owned by a user" do
Solution.should_receive(:find_by_user_id).with(@user.id).and_return(@solutions)
get :index, :id => @user.id
end
end
end
但是,这给我带来了以下错误:
ActionController::RoutingError in 'SolutionsController GET index should find all of the solutions owned by a user'
No route matches {:id=>nil, :controller=>"solutions", :action=>"index"}
任何人都可以告诉我如何测试这个,因为索引应该总是在特定用户的范围内调用?
I've got two models:
class Solution < ActiveRecord::Base
belongs_to :user
validates_attachment_presence :software
validates_presence_of :price, :language, :title
validates_uniqueness_of :software_file_name, :scope => :user_id
has_attached_file :software
end
class User < ActiveRecord::Base
acts_as_authentic
validates_presence_of :first_name, :last_name, :primary_phone_number
validates_uniqueness_of :primary_phone_number
has_many :solutions
end
with my routes looking like this:
map.resources :user, :has_many => :solutions
Now I'm trying to test my solutions controllers with the following RSpec test:
describe SolutionsController do
before(:each) do
@user = Factory.build(:user)
@solution = Factory.build(:solution, :user => @user)
end
describe "GET index" do
it "should find all of the solutions owned by a user" do
Solution.should_receive(:find_by_user_id).with(@user.id).and_return(@solutions)
get :index, :id => @user.id
end
end
end
However, this gets me the following error:
ActionController::RoutingError in 'SolutionsController GET index should find all of the solutions owned by a user'
No route matches {:id=>nil, :controller=>"solutions", :action=>"index"}
Can anybody point me to how I can test this, since the index should always be called within the scope of a particular user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Factory#build
构建该类的实例,但不保存它,因此它还没有 id。因此,
@user.id
为零,因为@user
尚未保存。由于
@user.id
为零,因此您的路线未激活。尝试使用
Factory#create
代替。Factory#build
builds an instance of the class, but doesn't save it, so it doesn't have an id yet.So,
@user.id
is nil because@user
has not been saved.Because
@user.id
is nil, your route isn't activated.try using
Factory#create
instead.看起来您的另一个问题就在这一行:
您正在尝试向索引方法发出请求,但您提供了错误的变量名称。当测试
SolutionsController
id
暗示解决方案 ID 时,您需要提供用户 ID。这应该有效,或者至少可以推动你前进:Looks like your other problem is on this line:
You're trying to make a request to the index method, but you've provided the wrong variable name. When testing
SolutionsController
id
implies a solution id, you need to supply the user id. This should work, or at least move you forward: