我有以下文件:

/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/spec/features/controller/spec_file_name.rb
 rspec/spec/features/controller_name.rb         #run all the specs in this controller

其他回答

你可以这样做:

 rspec/spec/features/controller/spec_file_name.rb
 rspec/spec/features/controller_name.rb         #run all the specs in this controller

有很多选择:

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

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

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

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

rspec spec / controllers / groups_controller_spec rb。

@ apne潜水的答案是一个整洁的解决这个问题的方法。然而,现在我们在Rspec 3.3中有了一个新方法。我们可以简单地运行rspec spec/unit/baseball_spec。Rb [#context:#it]而不是行号。从这里开始:

RSpec 3.3引入了一种识别示例的新方法[…] 例如,以下命令: $ rspec spec/unit/baseball_spec.rb[1:2,1:4] …将运行在spec/unit/baseball_spec.rb中定义的第一个顶级组下定义的第二个和第四个示例或组。

所以与其这样做 rspec规范/单位/ baseball_spec。第42行测试是第一个测试,我们可以简单地做 rspec规范/单位/ baseball_spec。Rb[1:1]或rspec spec/unit/baseball_spec。Rb[1:1:1]取决于测试用例的嵌套程度。

你可以使用

 rspec spec/controllers/groups_controller_spec.rb:<line_number>

行号应该是“描述”或“it”行的行号,这样它将运行该特定块中的测试。 相反,它将执行line_number旁边的所有行。

此外,您可以创建自定义名称的块,然后只能执行这些块。