Unit testing Rails applications' controller's helper Module - How?
In my rails application there exists a controller helper which is a Module (not a Class) with lot of helper methods defined in it. I wish to write unit test cases for this before I do code refactoring of this module so as to ensure that my understanding of the current system is right and that I have not broken any functionality inadvertantly. We are us开发者_JAVA技巧ing RSpec and I would wish to know how would one go about writing unit test cases for the methods of this helper Module?
Assume you have app/helpers/address_helper.rb
:
module AddressHelper
def full_address(address)
"#{address.street} #{address.house_number}/#{address.local_number}"
end
end
You can write spec under spec/helpers/address_helper_spec.rb
:
require 'spec/spec_helper' #or something similar, in order to load spec helper
describe AddressHelper do
describe "#full_address" do
it "displays the street, local and house number" do
address = stub('Address', :street => 'Mountain View', :house_number => '7', :local_number => '44')
helper.full_address(address).should eql('Mountain View 7/44')
end
end
end
Key here is helper
method, which gives you access to helper methods, in this example it is:
helper.full_address(address)
精彩评论