【问题标题】:Testing ActiveAdmin with rspec使用 rspec 测试 ActiveAdmin
【发布时间】:2012-05-30 09:37:30
【问题描述】:

我正在尝试开始测试 ActiveAdmin,特别是我需要测试来自 ActiveAdmin 控制器之一的 member_action。

你们知道关于这个主题的任何好的教程吗?

谢谢,

【问题讨论】:

    标签: rspec rspec2 activeadmin rspec-rails


    【解决方案1】:

    这就是我的工作方式:-

    app/admin/posts.rb

    ActiveAdmin.register Post do
    
      menu :parent => "Admin"
    
    
      #path = /admin/posts/:id/comments
      member_action :comments do
       @post = Post.find(params[:id])
      end 
    end
    

    spec/controllers/admin/posts_controller_spec.rb

    require 'spec_helper'
    include Devise::TestHelpers
    
    
    describe Admin::PostsController do
      render_views
    
      before(:each) do
        @user = mock_model(User, :email => "tester@localspecs.com")
        request.env['tester'] = mock(Tester, :authenticate => @user, :authenticate! => @user)
      end
    
      describe "Get comments" do
        before(:each) do
          @post = Post.create! valid_attributes
          Post.should_receive(:find).at_least(:once).and_return(@post)
          get :comments, :id => @post.id
        end
    
        after(:each) do
          @post.destroy
        end
    
        it "gets the proper record to update" do
          assigns(:post).should eq(@post)
        end
    
        it "should render the actual template" do
          response.should contain("Comments")
          response.body.should =~ /Comments/m
        end
      end
    end
    

    【讨论】:

    • 我收到一个错误“未定义的局部变量或方法 `request' for #<:core::examplegroup::nested_1:0xd765310>”
    • 我明白了,我必须将我的规范存储在规范/控制器中,以便 Rspec 自动将请求方法添加到我的规范中
    • 本例中的Tester是什么?
    【解决方案2】:
    # app/admin/post.rb
    ActiveAdmin.register Post do
    end
    
    # spec/controller/admin/posts_controller_spec.rb
    describe Admin::PostsController do
      subject { get :index }
      its(:status) { should eq 200 }
    end
    

    【讨论】:

    • 我遇到了这个错误:uninitialized constant Admin (NameError) 请帮忙。 :)
    • @across 那是因为你的命名空间可能不同,或者没有加载。也许您使用的是spec/spec_helper.rb 而不是spec/rails_helper.rb(一个新的堆栈溢出问题将是解决它的方法)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 2017-06-28
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多