我有以下文件:

/spec/controllers/groups_controller_spec.rb

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

我的宝石文件:

# 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 2的rails 3项目中,从rails根目录:

  bundle exec rspec spec/controllers/groups_controller_spec.rb 

应该没问题。我厌倦了输入,所以我创建了一个别名来缩短'bundle exec rspec'到'bersp'

'bundle exec'可以加载gem文件中指定的gem环境:http://gembundler.com/

Rspec2从'spec'命令切换到'rspec'命令。

其他回答

我使用这个守护宝石自动运行我的测试。它在对测试文件进行创建或更新操作后执行测试。

https://github.com/guard/guard-test

或 通常可以使用以下命令运行

rspec spec / controllers / groups_controller_spec rb。

不知道这个已经可用了多长时间,但是有一个Rspec配置用于运行过滤-所以现在你可以把它添加到你的spec_helper.rb:

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

然后在it、context或describe中添加一个focus标签,只运行该块:

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

Rake:

rake spec SPEC=path/to/spec.rb

(这个答案值得称赞。去给他投票吧。)

编辑(感谢@cirosantilli):要在规范中运行特定场景,必须提供与描述匹配的正则表达式模式匹配。

rake spec SPEC=path/to/spec.rb \
          SPEC_OPTS="-e \"should be successful and return 3 items\""

有很多选择:

rspec spec                           # All specs
rspec spec/models                    # All specs in the models directory
rspec spec/models/a_model_spec.rb    # All specs in the some_model model spec
rspec spec/models/a_model_spec.rb:nn # Run the spec that includes line 'nn'
rspec -e"text from a test"           # Runs specs that match the text
rspec spec --tag focus               # Runs specs that have :focus => true
rspec spec --tag focus:special       # Run specs that have :focus => special
rspec spec --tag focus ~skip         # Run tests except those with :focus => true

对于模型,它将只在第5行上运行case

bundle exec rspec spec/models/user_spec.rb:5

对于控制器:它将只在第5行上运行case

bundle exec rspec spec/controllers/users_controller_spec.rb:5

对于信号模型或控制器,从上面去掉行号

在所有模型上运行case

bundle exec rspec spec/models

在所有控制器上运行case

bundle exec rspec spec/controllers

运行所有案例

 bundle exec rspec