首页 文章

如何运行单个RSpec测试?

提问于
浏览
269

我有以下文件:

/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

Spec file:

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

12 回答

  • 59

    您可以将正则表达式传递给spec命令,该命令仅运行与您提供的名称匹配的 it 块 .

    spec path/to/my_spec.rb -e "should be the correct answer"
    
  • 424

    @apneadiving答案是解决这个问题的一种巧妙方法 . 但是,现在我们在Rspec 3.3中有了一个新方法 . 我们可以简单地运行 rspec spec/unit/baseball_spec.rb[#context:#it] 而不是使用行号 . 取自here:

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

    因此,我们可以简单地执行 rspec spec/unit/baseball_spec.rb[1:1]rspec spec/unit/baseball_spec.rb[1:1:1] 而不是执行 rspec spec/unit/baseball_spec.rb:42 (第42行中的测试),这取决于测试用例的嵌套方式 .

  • 1

    对于型号,它仅在第5行运行

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

    对于控制器:它仅在第5行运行

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

    对于信号模型或控制器,从上面删除行号

    在所有型号上运行案例

    bundle exec rspec spec/models
    

    在所有控制器上运行大小写

    bundle exec rspec spec/controllers
    

    运行所有案例

    bundle exec rspec
    
  • 1

    鉴于你在使用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'命令 .

  • 0

    通常我这样做:

    rspec ./spec/controllers/groups_controller_spec.rb:42
    

    42 表示我想要运行的测试行 .

    EDIT1:

    你也可以使用标签 . 见here .

    编辑2:

    尝试:

    bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42
    
  • 52

    从rspec 2开始,您可以使用以下内容:

    # in spec/spec_helper.rb
    RSpec.configure do |config|
      config.filter_run :focus => true
      config.run_all_when_everything_filtered = true
    end
    
    # in spec/any_spec.rb
    describe "something" do
      it "does something", :focus => true do
        # ....
      end
    end
    
  • 3

    我运行特定测试的首选方法略有不同 - 我添加了行

    RSpec.configure do |config|
        config.filter_run :focus => true
        config.run_all_when_everything_filtered = true
      end
    

    到我的spec_helper文件 .

    现在,每当我想运行一个特定的测试(或上下文或规范)时,我只需将标签"focus"添加到它,并正常运行我的测试 - 只有焦点测试才会运行 . 如果我删除所有焦点标签, run_all_when_everything_filtered 将开始并正常运行所有测试 .

    它不像命令行选项那么快速和简单 - 它确实需要您编辑要运行的测试的文件 . 但我觉得它让你有更多的控制权 .

  • 5

    有很多选择:

    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
    
  • 0

    随着耙子:

    rake spec SPEC=path/to/spec.rb
    

    (归功于this answer . 去投票吧 . )

    EDIT (感谢@cirosantilli):要在规范中运行一个特定方案,您必须提供与描述匹配的正则表达式模式匹配 .

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

    我使用这个guard gem来自动运行我的测试 . 它在测试文件上创建或更新操作后执行测试 .

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

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

    rspec spec / controllers / groups_controller_spec.rb

  • 0

    你可以这样做:

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

    在轨道5中,

    我用这种方式运行单个测试文件(所有测试都在一个文件中)

    rails test -n /TopicsControllerTest/ -v
    

    类名可用于匹配所需的文件 TopicsControllerTest

    我的 class class TopicsControllerTest < ActionDispatch::IntegrationTest

    输出:

    enter image description here

    如果你想要你可以调整正则表达式以匹配单个测试方法 \TopicsControllerTest#test_Should_delete\

    rails test -n /TopicsControllerTest#test_Should_delete/ -v
    

相关问题