文档章节

Rails RSpec Setup

Kolosek
 Kolosek
发布于 2018/02/07 22:52
字数 694
阅读 1
收藏 0

RSpec is an awesome tool for testing Rails apps. It is a hugely popular BDD-oriented (Behavior Driven Development) testing framework in the Ruby community.

It makes writing tests simpler, more expressive and easier to maintain!

Getting started

First, you'll need to install RSpec but you’ll also need Database Cleaner to help hold things together and ensure a clean state during tests. Add the following gems to the :test and :development groups in your Gemfile and then run bundle install:

group :development, :test do
  gem "database_cleaner"
  gem "rspec-rails"
end

Configuring RSpec

Before you can start with writing your RSpec tests your will need to run rails generate rspec:install. This will create the following files:

create  .rspec
create  spec
create  spec/spec_helper.rb
create  spec/rails_helper.rb

Files such as spec_helper.rb and rails_helper.rb contain the default RSpec set up with lots of comments. It is strongly recommended to read through all of those comments to get a good understanding of what each option does.

spec_helper.rb

spec_helper.rb file is used to configure RSpec. The base configuration file, after uncommenting some useful configuration options, is written in the following way:

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups
  config.filter_run_when_matching :focus
  config.disable_monkey_patching!
  
  if config.files_to_run.one?
    config.default_formatter = "doc"
  end
  
  config.profile_examples = 10
  
  config.order = :random
  Kernel.srand config.seed
end
  • expectations.include_chain_clauses_in_custom_matcher_descriptions = true - Chain method allows custom matcher descriptions and failure messages to include text for helper methods.
  • mocks.verify_partial_doubles = true - Partial doubles prevent you from stubbing any methods that don’t already exist on an object, it typo-proofs your mocks.
  • config.shared_context_metadata_behavior = :apply_to_host_groups - Shared context metadata configures how RSpec treats metadata passed as part of a shared example group definition.
  • filter_run_when_matching - Filter run when matching allows you to limit a spec run to individual examples or groups you tagged with :focus metadata.
  • disable_monkey_patching! - Prevents RSpec from monkey patching makes objects behave in tests as they would in "real" life.
  • default_formatter - Default formatter sets up the default format of your rspec tests that will be used if no formatter has been set.
  • config.profile_examples - Profile examples print the slowest examples and example groups at the end of the spec run, but it slows down your suite
  • config.order = :random and Kernel.srand config.seed - Configure tests to run in a random order. This helps to keep each test independent of one another.

rails_helper.rb

rails_helper.rb file is used for specs which depend on Rails (nearly every part of a Rails projects).
rails_helper.rb requires spec_helper.rb to work.

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
end
  • fixture_path - File fixture is a normal file stored in spec/fixtures by default.
  • use_transactional_fixtures - Transactional fixtures allow records created for one test to be visible for the next test.
  • infer_spec_type_from_file_location! - Allows to automatically tag specs in directories with matching type metadata so that they have relevant helpers available to them.
  • filter_rails_from_backtrace! - Backtrace filtering is used to filter out lines in backtraces that come from Rails gems in order to reduce the noise in test failure output.

Configuring Database Cleaner

By configuring the Database Cleaner in your application, you make it sure that tests start with a clean state. The only thing you need to do is to add the following to your RSpec.configure block in rails_helper.rb:

config.before(:suite) do
  DatabaseCleaner.clean_with(:truncation)
end

How to run the tests?

Once everything is configured you are ready to go! There are four ways to run your test:

  • Everything at once: bundle exec rspec.
    This runs all your tests.
  • One RSpec package: bundle exec rspec ./spec/models
    This runs all model specs.
  • One RSpec file at a time: bundle exec rspec ./spec/models/story_spec.rb.
    This runs only tests in Story model.
  • One by one: bundle exec rspec ./spec/models/story_spec.rb:10
    This runs only tests on line 10 in Story model.

Be careful! The more tests there are the longer it takes to compile them. Only run tests for what you really need!

We hope this article helped you get your tests up and running!

Originally published on kolosek.com .

 

© 著作权归作者所有

上一篇: React JSX
下一篇: RSpec Let Vs Before
Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
RSpec on rails 手记

环境:ruby 1.8.7 rails 2.3.5 1. 安装msysgit并将D:devmsysgitmsysgitbin给环境变量,否则以下的git插件不能安装 2. 安装haml: 1) gem install haml 2) config.gem "haml" ->config/envir......

阿昭
2010/12/05
870
0
RSPEC::RAILS介绍(翻译)

想有一下RSPec来做BDD开发,地发现中文资料太少了,无耐只能自己去主页上看资料,做为爱国者,俺的英文真烂得不成,不过翻完了,发出来大家乐一乐吧。 Spec::rails ------------------------...

阿昭
2010/12/01
2K
3
debian+ruby1.9.2+rails3.0.3+spork+rspec+cucumber

安装debian 2. 安装ssh 3. ssh中文问题 4. 安装ruby 最新的1.9.2要自己安装 5. gem问题 zlib 6. 安装rails 7. 安装LAMP 1) sudo apt-get install php5 2) sudo apt-get install mysql-serv......

阿昭
2011/01/28
627
0
行为驱动设计(rspec)

行为驱动设计(rspec) 原文:Behavior Driven Design (并非公开版权文档) 你可能已经注意到,验证模对象改变了你在单元测试中执行的那类测试的本质。传统的单元测试中,通过断言(Assertion)...

厚脸皮的tomsu
2010/03/13
1K
0
ruby on rails中如何使用rspec测试框架

从单元测试的角度为了保证我们写的函数,也就是从细粒度方便,来保证我们写的代码是没有问题的,所以测试还是很重要的。 测试本身呢,带来的好处也是非常多的。 1.比如说当我们项目中某一些依...

藤原佐为
2017/08/31
0
0

没有更多内容

加载失败,请刷新页面

加载更多

php7-internal 7 zval的操作

## 7.7 zval的操作 扩展中经常会用到各种类型的zval,PHP提供了很多宏用于不同类型zval的操作,尽管我们也可以自己操作zval,但这并不是一个好习惯,因为zval有很多其它用途的标识,如果自己...

冻结not
昨天
4
0
溢出\越界\泄漏

溢出:栈溢出是指函数中的局部变量造成的溢出,递归次数太多也会栈溢出 一是分配的大小超过栈的最大值,char a[99999999999999999]; 二是分配的大小没有超过最大值,但是接收的buff比新buff小...

SibylY
昨天
3
0
线性表-链式(c/c++实现)

在写数据的结构就是自己排兵布阵,根据客户的需求可增删功能,顺序与链式编程思想是一样的,两个比较只需要改变实现功能这一块。 线性表(链式)实现的三个步骤: 定义所需的功能(LinkList....

白客C
昨天
4
0
导览Linux系统文件系统类型

虽然对于普通用户来说可能并不明显,但在过去十年左右的时间里,Linux 文件系统已经发生了显著的变化,这使它们能够更好对抗损坏和性能问题。 如今大多数 Linux 系统使用名为 ext4 的文件系统...

老孟的Linux私房菜
昨天
4
0
Eureka重要对象简介

在进行分析EurekaClient和EurekaServer之间通信的源码之前,我们首先需要熟悉一下几个实体类 InstanceInfo 这个类代表着EurekaClient实例,客户端向服务端请求注册时就会携带着这个实例,这个...

Java学习录
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部