This article is indirect translation of Cucumber – obsluga kilku sesji – polish version (from andrzejsliwa.com devblog).
Most of standard testing related tasks can be achieved in a simple way using the default steps from Cucumber. By assumption Cucumber is for functional testing, but it can also be used to implement integration test. This is especially useful when we want test interaction between users, when they should get on-line notifications – and this should be tested without login/logout functionality, like in shout box which was popular some time ago.
When using integration tests you could use block open_session:
def login(user)
open_session do |sess|
sess.extend(CustomDsl)
u = users(user)
sess.https!
sess.post "/login", :username => u.username, :password => u.password
assert_equal '/welcome', path
sess.https!(false)
end
end
But in the case of cucumber which is based on the steps it was necessary to find a solution that is suited to the form in which scenarios are created.
So I have asked my friend Andrzej Sliwa to write an example code, this is the example:
module ActionController
module Integration
class Session
def switch_session_by_name(name)
if @sessions_by_name.nil?
@sessions_by_name = { :default => @response.session.clone }
end
@sessions_by_name[name.to_sym] ||= @sessions_by_name[:default].clone
@response.session = @sessions_by_name[name.to_sym]
end
end
end
end
Given /^session name is "([^\"]*)"$/ do |name|
switch_session_by_name(name)
end
Use of this mechanism is trivially easy to perform the following step:
Given session name is "guest no 1 session"
g
In this case, a named session is created which is not dependent on others (including the default). Access to default session is called by using the default name:
Given session name is "default"
You can always get back to named session using the same name as before, the state of it will be persisted for you.
More details on Integration tests: http://guides.rubyonrails.org/testing.html#integration-testing-examples