Tagging Your Test Cases
Here's a quick way to follow your tests a little more closely in your test.log. In your test setup, add the following line:
def setup RAILS_DEFAULT_LOGGER.debug "\n\e[0;31mRUNNING TEST CASE: #{name}\e[m\n" end
DZone Snippets > MattScilipoti > TDD
12363 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
def setup RAILS_DEFAULT_LOGGER.debug "\n\e[0;31mRUNNING TEST CASE: #{name}\e[m\n" end
rule "" do |t| t.name # ... do something with the name of the task end
## # Run a single test in Rails. # # rake blogs_list # => Runs test_list for BlogsController (functional test) # # rake blog_create # => Runs test_create for BlogTest (unit test) rule "" do |t| if /(.*)_([^.]+)$/.match(t.name) file_name = $1 test_name = $2 if File.exist?("test/unit/#{file_name}_test.rb") file_name = "unit/#{file_name}_test.rb" elsif File.exist?("test/functional/#{file_name}_controller_test.rb") file_name = "functional/#{file_name}_controller_test.rb" else raise "No file found for #{file_name}" end sh "ruby -Ilib:test test/#{file_name} -n /^test_#{test_name}/" end end
$ rake test:blog => Runs the full BlogTest unit test $ rake test:blog:create => Runs the tests matching /create/ in the BlogTest unit test $ rake test:blog_controller => Runs all tests in the BlogControllerTest functional test $ rake test:blog_controller:create => Runs the tests matching /create/ in the BlogControllerTest functional test
# Run specific tests or test files # # rake test:blog # => Runs the full BlogTest unit test # # rake test:blog:create # => Runs the tests matching /create/ in the BlogTest unit test # # rake test:blog_controller # => Runs all tests in the BlogControllerTest functional test # # rake test:blog_controller # => Runs the tests matching /create/ in the BlogControllerTest functional test rule "" do |t| # test:file:method if /test:(.*)(:([^.]+))?$/.match(t.name) arguments = t.name.split(":")[1..-1] file_name = arguments.first test_name = arguments[1..-1] if File.exist?("test/unit/#{file_name}_test.rb") run_file_name = "unit/#{file_name}_test.rb" elsif File.exist?("test/functional/#{file_name}_test.rb") run_file_name = "functional/#{file_name}_test.rb" end sh "ruby -Ilib:test test/#{run_file_name} -n /#{test_name}/" end end
copy "$(ProjectDir)App.config" "$(TargetPath).config"
expected tag, but no tag found matching {:attributes=>{:action=>"/admin/themes/update/1"}, :tag=>"form"} in: "<!DOCTYPE [...3k worth of crap cut...]</html>".
class ApplicationController < ActionController::Base def initialize(testing=false) super() self.class.layout(nil) if testing end [...] end and then in your functional tests: def setup @controller = MyController.new(true) [...] end
expected tag, but no tag found matching {:attributes=>{:action=>"/admin/themes/update/1"}, :tag=>"form"} in: "\n<ul>\n <li>Name: Blue</li>\n <li>Folder: blue</li>\n <li>Description: A Plain blue theme</li>\n <li>Masthead: Blue</li>\n <li>Editor: n/a</li>\n</ul>\n\n<a href=\"/admin/themes/show/1\">Cancel Editing</a>\n".
# Overrides the method +method_name+ in +obj+ with the passed block def override_method(obj, method_name, &block) # Get the singleton class/eigenclass for 'obj' klass = class <<obj; self; end # Undefine the old method (using 'send' since 'undef_method' is protected) klass.send(:undef_method, method_name) # Create the new method klass.send(:define_method, method_name, block) end # Just an example class class Foo def do_stuff "I'm okay!" end end # Test code list = [] 5.times { list.push(Foo.new) } # We override the method here! override_method(list.first, :do_stuff) { "I'm NOT okay!" } list.each_with_index { |f, i| puts "(#{i}) #{f.do_stuff}" } Outputs: (0) I'm NOT okay! (1) I'm okay! (2) I'm okay! (3) I'm okay! (4) I'm okay!