How do you run a single test/spec file in RSpec?

前端 未结 14 1658
-上瘾入骨i
-上瘾入骨i 2020-12-12 08:45

I want to be able to run a single spec file\'s tests — for the one file I\'m editing, for example. rake spec executes all the specs. My project is not a

相关标签:
14条回答
  • 2020-12-12 09:01

    This question is an old one, but it shows up at the top of Google when searching for how to run a single test. I don't know if it's a recent addition, but to run a single test out of a spec you can do the following:

    rspec path/to/spec:<line number>
    

    where -line number- is a line number that contains part of your test. For example, if you had a spec like:

    1: 
    2: it "should be awesome" do
    3:   foo = 3
    4:   foo.should eq(3)
    5: end
    6:
    

    Let's say it's saved in spec/models/foo_spec.rb. Then you would run:

    rspec spec/models/foo_spec.rb:2
    

    and it would just run that one spec. In fact, that number could be anything from 2 to 5.

    Hope this helps!

    0 讨论(0)
  • 2020-12-12 09:03

    from help (spec -h):

    -l, --line LINE_NUMBER           Execute example group or example at given line.
                                     (does not work for dynamically generated examples)
    

    Example: spec spec/runner_spec.rb -l 162

    0 讨论(0)
  • 2020-12-12 09:03

    Although many great answers were written to this question, none of them uses the Rspec tags approach.

    I use tags to run one or more specs in different files -- only those related to my current development task.

    For example, I add the tag "dev" with the value "current":

    it "creates an user", dev: :current do
      user = create(:user)
      expect(user.persisted?).to be_truthy
    end
    

    then I run

    bundle exec rspec . --tag dev:current
    

    Different tags/values can be set in individual specs or groups.

    0 讨论(0)
  • 2020-12-12 09:04

    specky.vim

    0 讨论(0)
  • 2020-12-12 09:10

    Lets say, you're running test for creating todo. You can always run that specific todo spec code using the file crete_spec.rb file as below.

          rspec/spec/features/controller/spec_file_name.rb
    
       Example:
    
       Creating  rspec spec/features/todos/create_spec.rb
       Editing   rspec spec/features/todos/edit_spec.rb
       Deleting  rspec spec/features/todos/destroy_spec.rb
    

    If you want to run all the specs in one single short.

       rspec 
    

    If you want to run all the specs in a specific controller user this.

      rspec/spec/feaures/controller_name
    
      Example:   rspec/spec/features/todos
    

    Hope it gives you more understanding!

    0 讨论(0)
  • 2020-12-12 09:11

    I was having trouble getting any of these examples to work, maybe because the post is old and the commands have changed?

    After some poking around I found this works:

    rspec spec/models/user_spec.rb

    That will run just the single file and provides useful output in the terminal.

    0 讨论(0)
提交回复
热议问题