【问题标题】:Rails 3 with Rspec2 and authlogic: Cannot spec requests & views带有 Rspec2 和 authlogic 的 Rails 3:无法指定请求和视图
【发布时间】:2010-09-04 11:42:20
【问题描述】:

我在为我的视图和请求创建规范时遇到问题。 我的一些控制器使用 named_scope,如下所示:

 #projects_controller.rb
 @projects = Project.with_user( current_user)

 ## project.rb:
 scope :with_user, lambda {|u|  {:joins => :client, :conditions => {:clients => {:user_id => u.id} } }}

但以下规范给出了错误:

规格:

describe "GET /projects" do
    it "works! (now write some real specs)" do
      get projects_path
    end 

错误:

6) Projects GET /projects works! (now write some real specs)
     Failure/Error: get projects_path
     Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
     # ./app/models/project.rb:9:in `block in <class:Project>'
     # /home/mping/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-3.0.0.rc/lib/active_record/named_scope.rb:95:in `call'
     # /home/mping/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-3.0.0.rc/lib/active_record/named_scope.rb:95:in `block in scope'
         # ./app/controllers/projects_controller.rb:4:in `index'

我的视图规范中有类似的错误:

 4) projects/show.haml renders attributes in <p>
     Failure/Error: render
     undefined method `name' for nil:NilClass
     # /home/mping/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-3.0.0.rc/lib/active_support/whiny_nil.rb:48:in `method_missing'
     # ./app/views/projects/show.haml:4:in `_app_views_projects_show_haml___1706343108022772066_34134280__4548622860215298334'

在我看来,我需要对 authlogic 定义的 current_user 方法进行存根,以便能够前进。我如何指定视图和请求?谢谢

【问题讨论】:

  • 切换到设计...

标签: ruby-on-rails ruby-on-rails-3 rspec authlogic bdd


【解决方案1】:

我刚刚在这里成功使用了解决方案:http://spacevatican.org/2011/12/5/request-specs-and-authlogic

before(:each) do
  activate_authlogic
  @user = FactoryGirl.create(:user)
  UserSession.create @user
  cookies['user_credentials'] = "#{@user.persistence_token}::#@user.send(@user.class.primary_key)}"
end

【讨论】:

    【解决方案2】:

    存根current_user 可能是一种令人沮丧的经历。您可以按照 Authlogic 文档中的建议,通过创建用户并登录来在规范中获得必要的会话功能。例如。

    include Authlogic::TestCase
    activate_authlogic
    @user = Factory.create(:user)
    UserSession.create(:user)
    

    退出:

    session = UserSession.find
    session.destroy if session
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多