8.4 rspec integration tests

in integration test, you can test many controllers at the same time,

 

test a complete workflow, like login to the home page, open signup page, fill in contents, and submit......

 

maybe you have made sure these functions worked fine through manual test, but the integration test is to make sure it remain working fine after you or other team member make big changes!

 

in this chapter, we will see the real power a integration test.

 

 

1. before we used to see integration test support the contoller test style constructions such as:

 

get '/'
response.should have_selector('title', :content => "Home")

 this is not the only kind of syntax supported.

 

rspec integration test support a highly expressive web-navigation syntax!!

like:

 

visit signin_path
fill_in "Name", :with => "Example User"
click_button

 

2. test users signup failure should not make a new user.

 

rails g integration_test users

 

require 'spec_helper'

describe "Users" do

  describe "signup" do

    describe "failure" do

      it "should not make a new user" do
        lambda do
          visit signup_path
          fill_in "Name",         :with => ""
          fill_in "Email",        :with => ""
          fill_in "Password",     :with => ""
          fill_in "Confirmation", :with => ""
          click_button
          response.should render_template('users/new')
          response.should have_selector("div#error_explanation")
        end.should_not change(User, :count)
      end
    end
  end
end
 

now it is clear what lambda is doing:

we want to make sure all the codes in the lambda block won't change the User.count.

 

so we put them into a lambda block then do the check on this block as a whole.

 

I'm still wondering where is the signup_path come from?????

it turns out that in the routes.rb


match '/signup', :to => 'users#new'


will auto define a named path


signup_path

 

 

ok, this integration test ties together all the different parts of Rails, including models, views, controllers, it provides an end to end verification that our signup machinery is working, cool, isn't it!!!

 

 

3. another integration test: users signup success should make a new user:

 

 

require 'spec_helper'

describe "Users" do

  describe "signup" do
    .
    .
    .
    describe "success" do

      it "should make a new user" do
        lambda do
          visit signup_path
          fill_in "Name",         :with => "Example User"
          fill_in "Email",        :with => "[email protected]"
          fill_in "Password",     :with => "foobar"
          fill_in "Confirmation", :with => "foobar"
          click_button
          response.should have_selector("div.flash.success",
                                        :content => "Welcome")
          response.should render_template('users/show')
        end.should change(User, :count).by(1)
      end
    end
  end
end

 BTW, when using fill_in, 

you can use the label, and you can also use the css id of the element.

(the first argument is the exact text users see in browser.)

this is very useful when there is no label.

 

the id is resource_name with the attr.

you can find the id by view html resource.

 

 

4. ok, 

git add .

git commit -m "user signup complete"

git checkout master

git merge signing-up

 

你可能感兴趣的:(integration)