【Rails】有关TDD测试入门
来源:互联网 发布:jquery移除数组元素 编辑:程序博客网 时间:2024/06/10 04:09
1.让 Rails 使用 RSpec 而不用 Test::Unit
rails generate rspec:install
2.为一些静态界面生成集中测试
$ rails generate integration_test static_pages
invoke rspec
create spec/requests/static_pages_spec.rb
可以很清楚的看到生成了static_pages_spec.rb这个文件 ,内容如下:
- require 'spec_helper'
- describe "StaticPages" do
- describe "GET /static_pages" do
- it "works! (now write some real specs)" do
- # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
- get static_pages_index_path
- response.status.should be(200)
- end
- end
- end
3.写一个测试首页内容的测试:
- require 'spec_helper'
- describe "Static pages" do
- describe "Home page" do
- it "should have the content 'Sample App'" do
- visit '/static_pages/home'
- expect(page).to have_content('Sample App')
- end
- end
- end
4若要测试正确运行,我们要在 spec_helper.rb 中加入一行代码
- RSpec.configure do |config|
- .
- .
- .
- config.include Capybara::DSL
- end
5.运行测试:
bundle exec rspec spec/requests/static_pages_spec.rb
可以看到我们的测试是失败的,因为首页是系统为我们生成的,不包含测试通过所需要的“Sample App”
将其修改便可以使得测试通过,这就是BDD,行为驱动测试,先写失败的测试,不断改进,使之成功。
如果写一个有关“About”页面的测试,开始提示路由失败,我们添加路由,提示action找不到错误,继续添加action,发现缺少模板,添加相关页面文件,最后测试变绿
0 0
- 【Rails】有关TDD测试入门
- 【Rails】有关TDD测试入门
- 【Rails】TDD-测试驱动开发
- 有关TDD的文章
- 有关TDD的Web
- 你的项目TDD了吗? 有关测试驱动开发的一点想法
- TDD与传统测试
- 测试驱动开发(TDD)
- TDD 测试驱动开发
- TDD相关测试框架
- TDD-测试驱动开发
- 测试驱动开发(TDD)
- TDD-测试驱动开发
- 测试驱动开发 TDD
- TDD 测试工具 (原创)
- 敏捷测试,TDD&ATDD
- 测试驱动开发TDD
- 测试驱动开发TDD
- 微信二维码扫描下载APK
- Firefox中的event is undefined
- IOS 学习八 代码创建UIButton
- C语言指针学习一
- cp和scp
- 【Rails】有关TDD测试入门
- python正则表达式的简单使用
- parentNode和offsetParent的使用
- Shell基础-Bash基本功能-多命令顺序执行与管道符
- 深度解读LinkedIn收购Lyndacom之敢问路在何方
- loadView、viewDidLoad及viewDidUnload的关系
- [Ruby on Rails Tutorial]01-使用Heroku部署应用程序
- Centos 6.6安装google chrome 出现/usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found 蹩脚解决方法
- Android Activity 降低屏幕亮度 Android开发