Hi,
I’ve got a lot of rspec controller tests. At the beginning of each
controller, I have:
def authenticate
…
end
def logout
…
end
Authenticates creates me a @current_user, logout destroys the
@current_user.
As I said, this code is at the beginning of each controller, not very
DRY
![:slight_smile: :slight_smile:](//test.ruby-forum.com/images/emoji/apple/slight_smile.png?v=6)
Any hints for me, where to put this code and how to integrate it in my
tests, to not always rewrite this two methods?
Regards
sewid
Authenticates creates me a @current_user, logout destroys the @current_user. As
I said, this code is at the beginning of each controller, not very DRY ![:slight_smile: :slight_smile:](/images/emoji/apple/slight_smile.png?v=6)
Any hints for me, where to put this code and how to integrate it in my tests, to
not always rewrite this two methods?
You could create a file in spec/support/current_user.rb (or whatever,
doesn’t matter as long as it’s in spec/support/*.rb) and put them in
there. Then they’ll be available to all your specs…
You can include helper modules in your spec_helper file:
RSpec.configure do |config|
config.include MySpec::SessionsHelper, :type => :controller,
:example_group => {
:file_path => config.escaped_path(%w[spec controllers])
}
end
This will include the MySpec::SessionsHelper in all of your controller
tests automatically. Then you just have to define your module in the
spec/support folder (which should be loaded by RSpec automatically if
you’re using the RSpec generated spec_helper file.)
spec/support/sessions_helper.rb
module MySpec
module SessionsHelper
def authenticate …
def logout …
end
end
S. Widmann wrote in post #981233:
Hi,
I’ve got a lot of rspec controller tests. At the beginning of each
controller, I have:
def authenticate
…
end
def logout
…
end