我正在尝试开始测试 ActiveAdmin,特别是我需要测试来自 ActiveAdmin 控制器之一的member_action。
你们知道关于这个主题的任何好的教程吗?
谢谢你,
这就是我所做的对我有用的方法:-
ActiveAdmin.register Post do
menu :parent => "Admin"
#path = /admin/posts/:id/comments
member_action :comments do
@post = Post.find(params[:id])
end
end
require 'spec_helper'
include Devise::TestHelpers
describe Admin::PostsController do
render_views
before(:each) do
@user = mock_model(User, :email => "[email protected]")
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
# 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
2024 年和 Rails v7 & v8 的答案
rails_helper.rb
RSpec.configure do |config|
config.include Devise::Test::IntegrationHelpers, type: :request
end
app/admin/dashboard.rb
ActiveAdmin.register_page 'Dashboard' do
content do
div class: 'blank_slate_container', id: 'dashboard_default_message' do
span class: 'blank_slate' do
span 'Welcome to the Admin'
end
end
end
end
spec/admin/dashboard_controller_spec.rb
RSpec.describe Admin::DashboardController, type: :request do
it 'redirects to login' do
get :admin_dashboard_path
expect(response).to redirect_to(new_admin_user_session_path)
end
context 'when logged in as admin' do
let(:admin) { create(:admin_user) }
before { sign_in(admin) }
it 'renders page' do
get :admin_dashboard_path
expect(response.body).to include('Welcome to the Admin')
end
end
end