`
peryt
  • 浏览: 55264 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

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

 

分享到:
评论

相关推荐

    RSpec.Essentials

    - RSpec is a behavior-driven development (BDD) framework for Ruby that allows developers to write clear and concise tests for their applications. - It emphasizes on describing the expected behavior ...

    rspec 入门者学习

    ### RSpec 入门者学习知识点详解 #### 一、RSpec 概述 RSpec 是一个流行的 Ruby 测试框架,主要用于行为驱动开发 (Behavior-Driven Development, BDD)。RSpec 的设计目的是让测试更加自然和易读,使得开发人员能够...

    the rspec book.pdf

    《RSpec Book》是一本专注于Rspec的权威指南,它详细阐述了如何使用Rspec这个强大的测试框架进行行为驱动开发(BDD)。Rspec是Ruby编程语言中的一个测试库,它使得编写可读性强、表达力丰富的测试代码成为可能。这...

    Writing Expressive Tests with RSpec

    RSpec is a popular tool for TDD with ruby. In this talk, we start with subject and let. Then we dive into should_receive vs. asserting side effects. Last part of this talk covers some traps and ...

    rspec测试.pdf

    《RSpec测试:行为驱动开发与RSpec、Cucumber及其他工具》 RSpec是一种用于Ruby语言的单元测试框架,它提倡一种称为“行为驱动开发”(Behavior Driven Development,BDD)的测试方式。RSpec允许开发者以自然语言的...

    rspec測試工具書

    最后,RSpec的使用通常与持续集成(Continuous Integration)和敏捷开发实践相结合,这可以提升软件开发的效率和软件质量的可靠性。通过RSpec,团队可以更快地获得反馈,及时发现和修复问题,从而在项目中实现更快的...

    RSpec 3.1中文版

    RSpec是Ruby语言开发的一款行为驱动开发(BDD)工具,它通过使用领域特定语言(DSL)来帮助开发人员编写测试用例。RSpec 3.1版本是RSpec框架的更新版,提供了更多的功能和更好的用户体验。Rails是一个用Ruby语言编写的...

    Pragmatic.The RSpec Book.2010.pdf

    #### 一、RSpec框架简介与特性 **RSpec** 是 Ruby 社区中最受欢迎的行为驱动开发(Behavior Driven Development, BDD)框架之一。它为开发者提供了一种灵活的方式来定义应用程序的行为,并通过简洁易读的语法来编写...

    rspec_api_documentation, 从RSpec自动生成API文档.zip

    rspec_api_documentation, 从RSpec自动生成API文档 RSpec Doc为你的Rails API生成漂亮的。查看一个示例文件。更改请查看维基以了解最新的更改。安装将rspec_api_documentation添加到你的文件gem 'rspec_a

    使用RSpec 测试Rails 程序.pdf

    ### 使用RSpec 测试Rails 程序的知识点总结 #### 一、RSpec与Rails结合的基础概念 **RSpec**(RSpec is not a unit testing framework)是一种为Ruby编程语言设计的行为驱动开发(BDD)框架,而**Rails**是基于...

    jruby-1.5.5+OperaWatir+RSpec

    标题 "jruby-1.5.5+OperaWatir+RSpec" 暗示了这是一个关于使用 JRuby 1.5.5 版本、OperaWatir 和 RSpec 进行自动化测试的项目或者资源集合。现在,我们将深入探讨这三个关键组件以及它们在 IT 领域中的应用。 JRuby...

    rspec-api-blueprint-formatter, 从RSpec测试自动生成API文档 !.zip

    rspec-api-blueprint-formatter, 从RSpec测试自动生成API文档 ! RSpec APIBlueprint格式化程序从RSpec测试自动生成API文档 !像这样it 'retrievs the patients medications' do retrieve_medications

    The Rspec Book 最新版 (带Cucmber章节) - 最好的BDD书籍

    《RSpec Book》是关于行为驱动开发(BDD)的一本权威书籍,特别是针对Rspec这一Ruby语言的测试框架。本书的最新版包含了Cucumber章节,使得读者能够更好地理解和实践BDD理念。 行为驱动开发(BDD)是一种软件开发...

    rspec-collection_matchers, 集合基数匹配器,从rspec期望中提取.zip

    rspec-collection_matchers, 集合基数匹配器,从rspec期望中提取 RSpec::CollectionMatchers RSpec::CollectionMatchers 让你在一个例子中表达一个对象集合的预期结果。expect(account.shopping_cart).to have_

    sensu-rspec-integration:该示例显示了Sensu(监视工具)和RSpec的基本集成

    Sensu-RSpec集成 这是如何将RSpec测试与Sensu集成的示例。 它使用了我对贡献的check-rspec Sensu检查的略微修改。 Sensu版本是0.19.2-1 ,仪表板( 0.9.0-1 )是0.9.0-1版本。 您可以在hieradata/common.yaml更改...

    The.RSpec.Book

    ### 《RSpec 书籍》:行为驱动开发与RSpec、Cucumber等工具的深入探索 #### 知识点一:RSpec 概述 - **RSpec**(RSpec)是一种为Ruby编程语言设计的行为驱动开发(Behavior Driven Development, BDD)框架。它通过...

    rspec_junit_formatter, RSpec结果格式化为你的CI可以读取的JUnit.zip

    rspec_junit_formatter, RSpec结果格式化为你的CI可以读取的JUnit RSpec JUnit格式化程序 RSpec 2 & 3结果, Jenkins可以读取。 可能还有其他的CI服务。灵感来自于的工作,在的RSpec格式化程序在对 Reporter的失望...

    Ruby-RSpec和Minitest匹配器来预防N1查询问题

    Ruby提供了两种强大的测试工具,RSpec和Minitest,它们通过匹配器功能可以帮助我们预防这种问题的发生。 RSpec是Ruby中广泛使用的BDD(行为驱动开发)框架,它允许开发者以自然语言的方式编写测试。匹配器是RSpec的...

Global site tag (gtag.js) - Google Analytics