in this chapter,we will see a very simple integration test to test routes in rails.
controller test is only able to test routes inside the controller, so if you want to test global routes, like home page "/home", "/about", you will need integration test, integration test can test across all controllers, it is like the browsers are accessing our app.
(testing routes is just the beginning of integration test.)
right now, our app doesn't have these routes, yet, we first write the test, then implement the route, this is so called test driven dev, TDD
we start by genrating a integration test:
rails generate integration_test layout_links
since we are using rspec framwork, this command will gen a file
spec/requests/layout_links_spec.rb
in rspec, integration test is called requests, no idea why.
(if you are not using rspec frameword, this command still work, it will gen file to test/integration/layout_links_test.rb)
in this test, we will get some urls, and make sure the right page are rendered.
so we will use
response.should have_selector("title", :content => "title")
below is the code:
require 'spec_helper'
describe "layoutlinks" do
it "should have a home page at '/'" do
get "/"
response.should have_selector("title", :content => "Home")
end
it "should have a about page at '/about'" do
get "/about"
response.should have_selector("title",:content => "About")
end
it "should have a Contact page at '/contact'" do
get '/contact'
response.should have_selector('title', :content => "Contact")
end
it "should have a Help page at '/help'" do
get '/help'
response.should have_selector('title', :content => "Help")
end
end
right now, all these test will fails, as we haven't added the routes yet.
(BTW, if you are running autotest, by default, autotest will not run integration test because it may be slow, if you do want autotest to run integration, (the writer find running integration test is better), you need config autotest in .autotest file
Autotest.add_hook :initialize do |autotest|
autotest.add_mapping(/^spec\/requests\/.*_spec\.rb$/) do
autotest.files_matching(/^spec\/requests\/.*_spec\.rb$/)
end
end
Don't worry about the API of autotest, I don't their mean, I just google to get it and use it.)
2. there is another very important test helper method in rspec:
"visit"
and
"click_link"
visit will visit the path,
click_link will click on the link.
for example:
it "should have the right links" do
visit root_path
click_link "About"
response.should have_selector("title", :content => "About")
end
Very cool, isn't it??
分享到:
相关推荐
- An in-depth look at TDD principles and how they can be applied effectively using RSpec. - Strategies for integrating TDD into the development workflow to improve code quality and reduce bugs. 5. ...
### 《RSpec 书籍》:行为驱动开发与RSpec、Cucumber等工具的深入探索 #### 知识点一:RSpec 概述 - **RSpec**(RSpec)是一种为Ruby编程语言设计的行为驱动开发(Behavior Driven Development, BDD)框架。它通过...
group :test do gem 'rspec' end ``` **基本使用:** 1. **编写测试案例**:使用 `describe` 和 `it` 块来组织测试用例,例如: ```ruby describe "Calculator" do it "should add two numbers correctly" ...
$ rake test rspec ...... Finished in 0.36783 seconds 6 examples, 0 failures 有关更多信息,请参阅或 rubycheck 的。 主页 Ruby RDOCS 安装 $ gem install rubycheck 执照 FreeBSD 要求 2.7.1+ 发展 签...
《RSpec测试:行为驱动开发与RSpec、Cucumber及其他工具》 RSpec是一种用于Ruby语言的单元测试框架,它提倡一种称为“行为驱动开发”(Behavior Driven Development,BDD)的测试方式。RSpec允许开发者以自然语言的...
《RSpec Book》是一本专注于Rspec的权威指南,它详细阐述了如何使用Rspec这个强大的测试框架进行行为驱动开发(BDD)。Rspec是Ruby编程语言中的一个测试库,它使得编写可读性强、表达力丰富的测试代码成为可能。这...
Test-DrivenExploration................ 285 18.4 DependencyRemoval.................. 288 18.5 Don’tLookBack..................... 297 19 PerformanceTestingandPerformanceImprovement 298 19.1 ...
mongoid-rspec库提供了RSpec兼容匹配器的集合,这些匹配器有助于测试Mongoid文档。 安装 将此行放入您的Gemfile中: group :test do gem 'mongoid-rspec' end 兼容性 该宝石与Mongoid 3、4、5、6和7兼容。 组态 ...
用法安装gem: gem install rspec_junit_formatter 用它: rspec --format RspecJunitFormatter --out rspec.xml 您将获得一个XML文件rspec.xml其中包含您的结果。 您也可以将其与其他结合使用: rspec --format ...
将此行添加到您的应用程序的Gemfile中: group :test do gem 'ngrok-rspec'end 然后执行: $ bundle 或将其自己安装为: $ gem install ngrok-rspec用法配置rspec RSpec . configure do | config | # any port can ...
rspec-mocks 是一个 rspec 的 test-double 框架,支持 method stubs, fakes 和生成 test-doubls 和类似真的对象的预期消息。 标签:rspec
将此添加到您的Gemfile中: gem 'rspec_api_helpers'然后执行: $ bundle或将其自己安装为: $ gem install rspec_api_helpers将其包含在您的spec_helper中 RSpec . configure do | config | # ... config . ...
rspec-2.x 是一个 meta-gem,依赖于其他组件:rspec-core, rspec-expectations 和 rspec-mocks 。每个组件都可以使用 gem 命令独立安装和运行。 标签:rspec
用法安装宝石: gem install rspec_org_formatter用它: rspec -f RspecOrgFormatter --out rspec.org您将获得一个包含结果的组织文件。更永久的使用如果您使用 ,请将其添加到您的 Gemfile 中。 在您的 .rspec 中,...
rspec-puppet, 针对 Puppet 清单的RSpec测试 你的Puppet的RSpec测试&模块 安装gem install rspec-puppet注意 ruby 1.8用户: 尽管 rspec-puppet本身支持 ruby 1.8,但你需要将rspec
Rails Rspec 包 在带有 rspec 的 rails 项目中,在 atom 编辑器中的 spec 和测试文件之间切换。 如何使用 在编辑器中: alt-ctrl-r :在规范和测试的 ruby 文件之间切换。... rails-rspec.specDefaultPath: 'spec'
Speaker: 鲁葳, an agile developer working for Neo. RSpec is a popular tool for TDD with ruby. In this talk, we start with subject and let... Last part of this talk covers some traps and caveats of RSpec.
RSpec supports Test Driven Development in Ruby through the BDD lens, keeping your focus on design and documentation while also supporting thorough testing and quick fault isolation. Cucumber, RSpec's...
您必须使用 RSpec.configure 配置格式化程序,通常在您的 spec_helper.rb 中。 这是 RspecHue 所需的最低配置(不是真的,Huey 会找到一个灯泡,但我不会指望它): RSpec . configure do | config | config . ...
使用RSpec轻松测试您的Rake任务。 安装 将此行添加到您的应用程序的Gemfile中: gem "fantaskspec" 然后执行: $ bundle install 然后在您的spec_helper或rails_helper : require "fantaskspec" RSpec . ...