For the beginning we would define helper for reading fixtures and put them into the spec/mailer_spec_helper.rb file:
1 2 3 4 5 6 7 8 9 | require File.dirname(__FILE__) + '/spec_helper.rb' module MailerSpecHelper private def read_fixture(action) IO.readlines("#{FIXTURES_PATH}/mailers/user_mailer/#{action}") end end |
Now we need to create fixtures for mailers in the spec/fixtures/mailers folder, each mailer in the separate subfolder like spec/fixtures/mailers/some_mailer/activation:
1 | Hello, Bob |
In spec/models/mailers/some_mailer_spec.rb we would write something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | require File.dirname(__FILE__) + '/../../mailer_spec_helper.rb' context 'The SomeMailer mailer' do FIXTURES_PATH = File.dirname(__FILE__) + '/../../fixtures' CHARSET = 'utf-8' fixtures :users include MailerSpecHelper include ActionMailer::Quoting setup do # You don't need these lines while you are using create_ instead of deliver_ #ActionMailer::Base.delivery_method = :test #ActionMailer::Base.perform_deliveries = true #ActionMailer::Base.deliveries = [] @expected = TMail::Mail.new @expected.set_content_type 'text', 'plain', { 'charset' => CHARSET } @expected.mime_version = '1.0' end specify 'should send activation email' do @expected.subject = 'Account activation' @expected.body = read_fixture('activation') @expected.from = '[email protected]' @expected.to = users(:bob).email Mailers::UserMailer.create_activation(users(:bob)).encoded.should == @expected.encoded end end |
That’s almost all. We are fully confident that emails look as we expected. In controller we don’t need to re-test mailers again, we just need to become convinced of mailer calling!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | context 'Given an signup action of UserController' do controller_name 'user' setup do @user = mock('user') @valid_user_params = { :email => '[email protected]' } end specify 'should deliver activation email to newly created user' do User.should_receive(:new).with(@valid_user_params).and_return(@user) Mailers::UserMailer.should_receive(:deliver_activation).with(@user) post :signup, :user => @valid_user_params response.should redirect_to(user_activate_url) end end |
I stopped getting confused with order of expected and real values. Compare:
1 | assert('login', @user.login) |
and
1 | @user.login.should == 'login' |
BTW, I made a mistake in the assert_equal parameters order when was writing this article.
I could understand what objects should do at first look on the test:
1 2 3 4 5 6 7 | context 'The User model with fixtures loaded' do specify 'should be friended by the user if this user added him to friends' do users(:bob).friends << users(:chris) users(:chris).should have(1).friended users(:chris).friended.first.should == users(:bob) end end |
Test results are presented in easy to understand and clear form, just simple sentences, which are making asserts about entities in the system. In most cases there is even no necessity to look at the tests to understand which functionality has one or another entity!
1 2 3 4 5 6 7 8 9 | The newly created User - should be valid with all neccessary attributes specified - should be invalid without login - should be invalid when login length is less than 2 - should be invalid without email - should be invalid with wrong email - should be invalid with someone else's login - should be invalid with someone else's email - should not require password and password_confirmation |
I started writing tests before actual coding finally! In fact, every developer think over what one or another method should do, and frequently these thoughts looks like “when method received string ‘somestring’ it should return ‘anotherstring'”. It’s easy to describe these thoughts using BDD-tests.
Integrated flexible mock-objects system allows testing functionality just once. For example, when you test controllers, you should not use database and models – it is sufficient to check, if necessary model methods would be called in right order with right parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | context 'Given an signup action of UserController' do controller_name 'user' setup do @user = mock('user') add_stubs(@user, :password= => nil, :password_confirmation= => nil, :new_password= => nil) end specify 'should render edit template with empty User object' do User.should_receive(:new).with(:no_args).and_return(@user) get :signup response.should render_template(:edit) assigns[:user].should be(@user) end end |