For a newbie-Please explain odd RSpec behavior

I am doing an extremely basic tutorial on Ruby in RubyMine, and as part of the tutorial, we create a Card class in a .rb Ruby file. In addition to the class definition, the tutorial has us put an Rspec test in the same .rb file. The .rb file looks like the following:


require 'rspec'

class Card
    def value
        5
    end
end

describe Card do
    it "should have a value of 6" do
        Card.new.value.should eq(7)
    end
end

In RubyMine, if I right-click the project folder and "Run all specs in the project", I get errors and messages including "0 files found" and "Empty test suite." Also, if I just run the rb script, I get no errors...the script runs fine.

The STRANGE thing is that if I add a second file to the project called card_spec.rb and put in it only the test from above:

require 'rspec'
require_relative 'thing'

describe Card do
    it "should have a value of 6" do
        Card.new.value.should eq(7)
    end
end

And then right-click the project and select "Run all specs," the tests run fine. Notice I said the tests (plural)...RubyMine will run the test in card.rb (which it did not see before) AND the test in card_spec.rb.

Why won't it recognize the test in card.rb without my having to add a second file called card_spec.rb?? This makes no sense to me. Is there anyway I could have it run the rspec tests from within card.rb without having to create a second file?

I work on a Mac, have RubyMine version 4.5, using ruby interpreter 1.9.3, rspec 2.12.0.

3 comments
Comment actions Permalink

Hi,

by default rspec run configuration uses *_spec.rb pattern to find all files which may contain specs.
This is why RM finds no specs in the first case.
In the second case RM finds card_spec.rb and loads it, this file also loads thing.rb which declares the second spec so in this case we have two specs to execute.
Hope this will clarify the situation for you.
To fix the first case you can either modify pattern to select files or always place specs in *_spec.rb (which is preferred approach since rspec uses the same pattern as RM)

Regards, Oleg.

0
Comment actions Permalink

Oleg--Thank you for your response.  That explains everything.  

Just curious, even though it is not the convention or preferred method, how does one modify the pattern to select files?  I am not planning to do so, but it would be interesting to know how it is done (just to learn).



My intention was to mark your response as helpful AND as the answer.  However, after I marked it as helpful, the option to mark as answer was removed.  I think the system only allows for one to be selected. If you reply regarding changing the pattern, I will mark it as the answer.
0
Comment actions Permalink

Hi,

to edit the pattern go to Run|Edit Configuration, select the run configuratin you are going to change, change "File name mask" from "**/*_spec.rb" to something else.

Regards, Oleg.

0

Please sign in to leave a comment.