Testing AuthLogic with RSpec and Mock Helpers

AuthLogic, RSpec, Ruby on Rails No Comments »

Note: This all concerns Rails 2.3.x and recent versions of AuthLogic

Testing AuthLogic with RSpec is still a puzzle with several parts that can be difficult to fit together. Very helpful is a sample project on github that demonstrates several testing options with RSpec.

A series of mock helper methods that you can stuff in your spec_helper.rb can ease testing, letting you call

login

or

current_user

anywhere in your specs.

If you get errors such as

Mock "UserSession_1002" received unexpected message :record with (no args)

when mocking, the issue might be that current_user_session.record (or the underlying method which is hard to find) which is likely called in your ApplicationController is deprecated in favour of current_user_session.user

This deprecated code is still in the authlogic sample project as well as Ryan Bates’ Railscast 160, so it might be confusing. Alternatively you can mock the :record call as well as some have accomplished as posted in the comments of the original post.

 

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Full Trace with RSpec

RSpec No Comments »

Just a reminder: To get a the full backtrace when running RSpec, either use the -b option,

script/spec -b /path/to/spec.rb 

or add

--backtrace

to your spec.opts. Here are the other options for either the opts file or the command line.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

RSpec and Webrat for Rails Integration Testing

RSpec, Ruby on Rails, Webrat No Comments »

Getting RSpec and Webrat to cooperate has gotten a bit easier recently, so make sure you have all the latest versions of the webrat, rspec and rspec-rails gems installed. Some quirks remain which might trip you up coming from TestUnit. For example if you

cd spec/integration

and

spec ./user_integration_spec.rb

or

ruby ./user_integration_spec.rb

you might get

undefined method `visit (…)

while a similar procedure would run fine in test/integration with TestUnit

But with RSpec, when you run

spec ./spec/integration/user_integration_spec.rb

or

rake spec:integration

all is well. It seems you have to stand at least in the spec directory or above, otherwise the block

Webrat.configure do |config|
  config.mode = :rails
  config.open_error_files = false # prevents webrat from opening the browser
end

in spec_helper.rb will not run correctly.

So remember that RSpec is a bit less forgiving than TestUnit in how your tests are called.

It’s also important that you run

script/generate rspec

after installing RSpec to have the rake tasks “installed”.

Finally, his procedure will likely not be necessary with recent versions of Webrat and RSpec.

See here for a testing system for Rails that integrates RSpec and Webrat, it makes a  lot of sense.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in