开发者

How to use mock_model in spec_helper (for use with Authlogic)

开发者 https://www.devze.com 2023-03-18 06:13 出处:网络
I\'m upgrading to rails 3 and rspec 2. I\'m getting the error \"undefined method `mock_model\'\" in my spec_helper. My current_user method sets up a mock user for authlogic and generates this error.

I'm upgrading to rails 3 and rspec 2.

I'm getting the error "undefined method `mock_model'" in my spec_helper. My current_user method sets up a mock user for authlogic and generates this error.

My spec helper looks like this

require 'authlogic/test_case'
require 'shoulda'
require 'helpers'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
end

module LoginHelper
   include Authlogic::TestCase

   def login_user person
     activate_authlogic
     UserSe开发者_StackOverflow中文版ssion.create(...)
   end
end
include LoginHelper


def current_user(stubs = {})
  @current_user ||= mock_model("User", stubs)
end

def user_session(stubs = {}, user_stubs = {})
  @current_user ||= mock_model(UserSession, {:user => current_user(user_stubs)}.merge(stubs))
end

def login(session_stubs = {}, user_stubs = {})
  UserSession.stub!(:find).and_return(user_session(session_stubs, user_stubs))
end

def logout
  @user_session = nil
end


mock_model is defined in RSpec 2 in the rspec-rails gem. If you added rspec-rails to your Gemfile, then it should automatically be included for use with all the other necessary RSpec 2 gems.

You should also note that mock_model won't work with UserSession, as it inherits from Authlogic::Session::Base, not ActiveModel. You can use fix this by adding to your UserSession model:

class UserSession < Authlogic::Session::Base
  extend ActiveModel::Naming
end
0

精彩评论

暂无评论...
验证码 换一张
取 消