使用关联测试 Rspec 控制器

发布于 2024-08-22 06:55:26 字数 1322 浏览 3 评论 0原文

我有两个模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌能量女王 2024-08-29 06:55:26

Factory#build 构建该类的实例,但不保存它,因此它还没有 id。

因此,@user.id 为零,因为 @user 尚未保存。

由于 @user.id 为零,因此您的路线未激活。

尝试使用 Factory#create 代替。

  before(:each) do
    @user = Factory.create(:user)
    @solution = Factory.create(:solution, :user => @user)
  end

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.

  before(:each) do
    @user = Factory.create(:user)
    @solution = Factory.create(:solution, :user => @user)
  end
初吻给了烟 2024-08-29 06:55:26

看起来您的另一个问题就在这一行:

 get :index, :id => @user.id

您正在尝试向索引方法发出请求,但您提供了错误的变量名称。当测试 SolutionsController id 暗示解决方案 ID 时,您需要提供用户 ID。这应该有效,或者至少可以推动你前进:

 get :index, :user_id => @user.id

Looks like your other problem is on this line:

 get :index, :id => @user.id

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:

 get :index, :user_id => @user.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文