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 => "user@example.com"
          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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值