rails 测试框架 rspec 及 shoulda-matchers 的配置及使用

 在ruby rails 开发中,测试时无可避免的,TDD策略的确可以提高程序员的代码质量及通过率。

 下面分享也是给自己的一个加重印象讲一下rspec及 should-matchers的配置;其中 rspec是主要的测试框架, 而 shoulda-matchers依赖 rspec测试框架。

 博主以为其实rails程序的开发跟Node.js的开发模式差不多,都是要什么装什么。在rails开发中, 我们通过编辑Gemfile文件来添加需要的gem包文件;

 可以参看下图,我们测试所需要的gem包都在 group :development, :test 包含下添加;

还有一种添加方式通过Terminal命令窗口添加,我们只需要输入

gem install rspec shoulda-matchers

一般情况下,我们在初始化一个项目(rails new)的时候,默认会添加一个test文件夹,我们可以删除,然后通过 

1 rails generate rspec:install

我们就可以在项目根路径文件夹下看到rspec文件夹,Terminal也会显示如图所示

rspec测试文档及shoulda-matchers测试文档:

rspec: https://relishapp.com/rspec/rspec-rails/v/3-4/docs/gettingstarted

shoulda-matchers: http://thoughtbot.github.io/shoulda-matchers/v2.7.0/

PS 注意shoulda-matchers还要配置rails-helper.rb文件

添加:

 

require 'shoulda/matchers'

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :active_record
    with.library :active_model
    with.library :action_controller
    with.library :rails
  end
end

 

posted @ 2016-02-18 16:42  路杰2015  阅读(277)  评论(0编辑  收藏  举报