| Module | ActiveResourceIntegrationDsl::SessionMethods |
| In: |
lib/active_resource_integration_dsl.rb
|
All of the following methods apply to sessions created with new_session or new_session_as. All samples on this page are assumed to exist inside a new_session_as block with user representing the session object. For instance…
new_session_as('user','password') do |user|
user.lists_tickets # 'user' does GET to tickets_path
end
The standard style of writing integration tests with ARID is very similar to using RESTful path helpers. Typical Rails magic deduces what you are trying to test based on the method you call on your session object. Several session instance methods are also provided should your unique scenario prevent the use of the magic methods, but in most cases where you are following Rails conventions, the magic methods should be sufficient.
All Arid magic methods begin with one of several prefixes that begin to describe what the logged in user will be doing. These prefixes are shown in the following, list with their corresponding controller actions. Each will be explained in more detail below.
Each of the above prefixes is followed by a description of the action that is derived from the RESTful helpers provided by map.resources calls in the routes.rb. For instance, if the _path method you would use in a link_to method in your view was something like new_article_comment_path(@article) then a corresponding ARID test would look something like user.builds_article_comment(@article). Or, if the form tag to create a comment looked something like <% form_for :comment, :url => comments_path do |form| %> then the corresponding ARID test to test the submission of this form would look something like user.creates_comment(:params => {:comment => {:subject => ‘Wow!’, :body => ‘This is cool!’}}). Note how in this last example, the path method in the form tag uses _comments, (plural) but the ARID uses _comment (singular).
All ARID methods accept a block that you pass the response to for doing application specific assertions. For instance…
user.shows_article(@article) do |page|
page.assert_select "div[id='title']", @article.title
end
Each of these perform a GET request to either the index or show actions of the controller, depending on the path provided. These are actually aliases for the same method and are technically interchangeable. Determining which controller action gets called is based entirely on the given path and your settings in routes.rb. It is recommended that you use the prefix that ‘sounds’ correct in the context of the controller action you are testing. For example, user.lists_comment(1) and user.shows_comment(1) would both trigger the show action and work equally well, but only one sounds correct. Likewise, user.lists_comments and user.shows_comments both sound correct, but one should sound more correct, especially to an experienced Rails developer.
After any ids or ActiveRecord objects necessary to generate the path, you may include a hash with any of the following keys.
Trigger the new or edit action in the controller respectively. Optionally take a :params hash option which unlocks more magic (see below).
After any ids or ActiveRecord objects necessary to generate the path, you may include a hash with any of the following keys.
If you provide a :params hash option, ARID will also assert that the reponse includes a form on the page, and assert that the form includes…
Next ARID will forward all of your arguments to creates_ or updates_ to submit the form, assert the response is a :redirect, follow the :redirect and assert next response is :success.
Trigger the create or update action in the controller respectively.
After any ids or ActiveRecord objects necessary to generate the path, you may include a hash with any of the following keys.
:params => {
:user => {
:username => 'captainbuggernuts',
:password => 'mychippies'
}
},
:expects => :success) do |page|
page.assert_select "div[id='error_messages']", 'Login Failed'
end
Triggers destroy action in controller.
After any ids or ActiveRecord objects necessary to generate the path, you may include a hash with any of the following keys.
This test is usually only useful on the most basic controllers, ie, scaffold generated and mostly unedited. But for those controllers, it‘s a quick easy way to boost your test coverage. It will walk through the entire process of creating, updating, and destroying an object, making standard assertions along the way.
Resort to this only when unable to use magic methods described above.
# File lib/active_resource_integration_dsl.rb, line 272
272: def builds(object,*args,&block)
273: opts = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
274: params = opts.delete(:params)
275: goes_to(new_path_for(object,args)) do |page|
276: check_form_on(page,self.send("#{object.to_s.pluralize}_path",*args),:post,params,opts) if params
277: end
278: creates(object,*args + [opts.merge(:params => params)],&block)
279: end
Resort to this only when unable to use magic methods described above.
# File lib/active_resource_integration_dsl.rb, line 285
285: def creates(object,*args,&block)
286: opts = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
287: params = opts.delete(:params)
288: posts_to(path_for(object.to_s.pluralize,args),params,opts,&block)
289: end
Resort to this only when unable to use magic methods described above.
# File lib/active_resource_integration_dsl.rb, line 318
318: def destroys(object,*args,&block)
319: opts = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
320: deletes_to(path_for(object,args),opts,&block)
321: end
Resort to this only when unable to use magic methods described above.
# File lib/active_resource_integration_dsl.rb, line 295
295: def edits(object,*args,&block)
296: opts = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
297: params = opts.delete(:params)
298: goes_to(edit_path_for(object,args)) do |page|
299: check_form_on(page,path_for(object,args),:put,params,opts) if params
300: end
301: updates(object,*args + [opts.merge(:params => params)],&block)
302: end
Resort to this only when unable to use magic methods described above.
# File lib/active_resource_integration_dsl.rb, line 262
262: def shows(object,*args,&block)
263: opts = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
264: goes_to(path_for(object,args),opts,&block)
265: end
Resort to this only when unable to use magic methods described above.
# File lib/active_resource_integration_dsl.rb, line 308
308: def updates(object,*args,&block)
309: opts = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
310: params = opts.delete(:params)
311: puts_to(path_for(object,args),params,opts,&block)
312: end