欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

rspec with cancan/devise/Webrat

http://stackoverflow.com/questions/4200343/rspec-cancan-and-devise

1、 controller
def mock_user(stubs={})
@mock_user ||= mock_model(User, stubs).as_null_object
end

def mock_link(stubs={})
(@mock_ak_config ||= mock_model(User).as_null_object).tap do |user|
user.stub(stubs) unless stubs.empty?
end
end

before(:each) do
# mock up an authentication in the underlying warden library
request.env['warden'] = mock(Warden, :authenticate => mock_user,
:authenticate! => mock_user)
@ability = Object.new
@ability.extend(CanCan::Ability)
controller.stub(:current_ability) { @ability }
end
2、 spec_helper.rb
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr

# Remove this line if you’re not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = “#{::Rails.root}/spec/fixtures”

# If you’re not using ActiveRecord, or you’d prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.include Devise::TestHelpers, :type => [:controller, :views]
config.include ControllerHelpers, :type => :controller
end

Webrat.configure do |config|
config.mode = :rails
end

发表评论