rails 安装 rspec - gem ‘rspec-rails’
创建rspec配置文件 - rails g rspec:install
should与should_not - 对象如有valid?之类的方法,test可这么写model.should be_valid或model.should_not be_valid - model.should == xx 等同 model.should eq xx
expect - expect(model).to eq some_thing
let与before - let与before(:each)的一个区别是before可以初始化变量,let不能before可以这样
1 2 3 | |
- before(:each) 每个测试相互独立,分别调用这个
- before(:all)只调用这个一次
- let可用于定义变量,如下,在测试用例中可使用 king_of_zombie变量
1 2 3 | |
Load RSpec 2.x support by adding the following line (typically to your spec_helper.rb file): require ‘capybara/rspec’ If you are using Rails, put your Capybara specs in spec/features. If you are not using Rails, tag all the example groups in which you want to use Capybara with :type => :feature. You can now write your specs like so: describe “the signin process”, :type => :feature do before :each do User.make(:email => ‘user@example.com’, :password => ‘caplin’) end
1 2 3 4 5 6 7 8 9 10 | |
http://rubydoc.info/gems/rspec-rails/file/Capybara.md https://github.com/rspec/rspec-rails
Many users have been confused by the co-existence of the the Capybara::DSL (visit/page) alongside the rack-test DSL (get|post|put|delete|head/response.body) in examples in spec/requests and spec/controllers. As of rspec-rails-2.11.1 and capybara-2.0.0.beta2, these are separated as follows:
Capybara::DSL is included - spec/features
rack-test DSL is included in - spec/requests and spec/controllers
Capybara::RSpecMatchers is added to examples in: - spec/features - spec/controllers - spec/views - spec/helpers - spec/mailers
2012年6月,Rspec开发团队宣布,在v2.11中使用了新句法 来替代传统的should式句法,如
1 2 3 | |
新句法会把要测试的值传递给 expect() 方法,然后和匹配器比较:
1 2 3 | |
当有多个it “xxxx” do end 而测试的主题一致时, 可用
1 2 3 | |