如何运行单个 RSpec 测试? [英] How to run a single RSpec test?

查看:40
本文介绍了如何运行单个 RSpec 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件:

/spec/controllers/groups_controller_spec.rb

我在终端中使用什么命令来运行该规范以及在哪个目录中运行该命令?

What command in terminal do I use to run just that spec and in what directory do I run the command?

我的宝石文件:

# Test ENVIRONMENT GEMS
group :development, :test do
    gem "autotest"
    gem "rspec-rails", "~> 2.4"
    gem "cucumber-rails", ">=0.3.2"
    gem "webrat", ">=0.7.2"
    gem 'factory_girl_rails'
    gem 'email_spec'
end

规范文件:

require 'spec_helper'

describe GroupsController do
  include Devise::TestHelpers

  describe "GET yourgroups" do
    it "should be successful and return 3 items" do

      Rails.logger.info 'HAIL MARRY'

      get :yourgroups, :format => :json
      response.should be_success
      body = JSON.parse(response.body)
      body.should have(3).items # @user1 has 3 permissions to 3 groups
    end
  end
end

推荐答案

不确定此功能可用多长时间,但有用于运行过滤的 Rspec 配置 - 所以现在您可以将其添加到您的 spec_helper.rb:

Not sure how long this has bee available but there is an Rspec configuration for run filtering - so now you can add this to your spec_helper.rb:

RSpec.configure do |config|
  config.filter_run_when_matching :focus
end

然后在 itcontextdescribe 中添加一个焦点标签以仅运行该块:

And then add a focus tag to the it, context or describe to run only that block:

it 'runs a test', :focus do
  ...test code
end

RSpec 文档:

https://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#filter_run_when_matching-instance_method

这篇关于如何运行单个 RSpec 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆