0

如何在 RSpec for Controller 中纠正此错误,

1) SellersController GET 索引查找活动失败/错误:assigns(:activity).should eq([activity])

   expected: [#<Activity id: 65, transactable_type: "admin", transactable_id: 1, action_type: "seller", user_id: 1, is_approved: false, approved_by: nil, created_at: "2012-04-09 11:02:17", updated_at: "2012-04-09 11:02:17", associatable_type: nil, associatable_id: nil>]
        got: nil

   (compared using ==)

Seller_rspec.rb

describe "GET index" do
    it "find the Activity" do
 activity = Activity.create!(:transactable_type=>"admin",:transactable_id=>1,:action_type=>"seller",:user_id =>1,:is_approved=>0)
     get :index,{:is_approved => activity.to_param,:user_id=>1,:approved_by=>"admin"}
     assigns(:activity).should eq([activity])
    end

在控制器中

 def index
        @activities=Activity.find(:all,:select => 'DISTINCT transactable_type,transactable_id,action_type,is_approved,approved_by',:conditions=>["is_approved= ?  and user_id=? and approved_by IS NULL",false,current_user.id])
  end
4

1 回答 1

0

您正在将代码放入控制器中,该控制器应该进入模型。在 Activity 模型中创建一个方法或范围,例如:

def self.find_not_approved(current_user_id)
      find(:all,
           :select => 'DISTINCT transactable_type,transactable_id,action_type,is_approved,approved_by',
           :conditions= ["is_approved= ?  and user_id=? and approved_by IS NULL",
           false,
           current_user_id])
end

所以你可以在控制器中拥有(我已经组成了方法名称):

  def index
    @activities = Activity.find_not_appoved(current_user.id)
  end

只是为了回答您的问题,它不应该是- 因为您正在检查控制器中的 @activities 变量而不是 @activity assigns(:activities).should eq([activity])assigns(:activity).should eq([activity])

于 2012-04-09T11:54:17.773 回答