0
我想爲我繼承的ActiveAdmin控制器編寫測試,並且在檢查實例變量正在調用方法時遇到問題。我想要的行爲是調用方法的副作用,所以我想確保它被調用。rspec控制器測試實例變量調用方法
這裏的控制器:
ActiveAdmin.register User do
controller do
before_filter :get_user
def get_user
@user = User.find_by_login(params[:id])
if [email protected]
flash[:notice] = "No user found with that login"
return false
end
end
end
member_action :tos_ban, :method => :post do
@user.ban!(current_user, "admin ban", 1)
flash[:notice] = "#{@user.display_name} successfully TOS Banned"
redirect_back
end
end
而且,我的規格:
require 'spec_helper'
describe ActiveAdmin::UsersController do
render_views
let(:user) { FactoryGirl.create(:user, :admin => admin) }
let(:administrator) { FactoryGirl.create(:administrator) }
let(:banned_user) { FactoryGirl.create(:user) }
before(:each) do
login_as(user)
session[:last_admin_action_time] = Time.now
session[:current_admin_id] = administrator.id
end
describe 'banning users' do
context 'as an administrator' do
let(:admin) { true }
before(:each) do
User.should_receive(:ban!).once # I want something like this
# on @user in the controller
post :tos_ban, :id => banned_user.login
end
it 'bans the right user' do
assigns(:user).should eq(banned_user)
end
end
end
end
編輯:
所以,有點RTFMing後 - 通過RSpec的書 - 我發現我需要使用mocks:
it 'bans the user' do
banned_user = mock_model(User)
User.stub(:find_by_login).and_return(banned_user)
banned_user.should_receive(:ban!)
banned_user.should_receive(:display_name)
post :tos_ban, :id => banned_user.login
end