Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test helpers in Rails?

I'm trying to build some unit tests for testing my Rails helpers, but I can never remember how to access them. Annoying. Suggestions?

like image 752
aronchick Avatar asked Jan 13 '09 20:01

aronchick


People also ask

How do I run a test in Rails?

2.7 The Rails Test Runner Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case. You can also run a particular test method from the test case by providing the -n or --name flag and the test's method name.

How do I test a Rails controller?

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response. Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework.

How do helpers work in Rails?

A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words . This method is helpful whenever you want to display time in this specific format.


1 Answers

In rails 3 you can do this (and in fact it's what the generator creates):

require 'test_helper'  class YourHelperTest < ActionView::TestCase   test "should work" do     assert_equal "result", your_helper_method   end end 

And of course the rspec variant by Matt Darby works in rails 3 too

like image 123
Eugene Bolshakov Avatar answered Oct 11 '22 13:10

Eugene Bolshakov