RSpec w/Rakefile & spec_helper configuration?
So I've never used RSpec or Rake before and I've been having a bunch of trouble with this. Partly because there seems to have been some non-backwards compatible changes to RSpec which makes it hard to find good examples around that I know will work. I just updated to RM7.0. Right now I just keep getting empty test suite messages. It just doesn't seem to be running the spec files at all. I'm fairly sure this has to do with me not understanding Rake and/or the file structures. I get message that the coverage is 100% and 0/0 lines are covered.
My project is pretty simple with this kind of structure:
/ProjectHome
-/lib
-code1.rb
-code2.rb
-/spec
-code1_spec.rb
-code2_spec.rb
-both_spec.rb
-spec_helper.rb
-Rakefile
What I'm trying to do:
1. I need to run SimpleCov for all of my tests. SimpleCov works if I put SimpleCov.start inside my lib files so I know the gem is installed and working.
2. I need a way to run the spec tests and SimpleCov that will work on a system without RubyMine so that's why I think I need a rakefile and/or a spec_helper
If I right click on the 'spec' folder and run the specs directly through RubyMine they run so I know rspec is installed working to some degree.
3. I wouldn't mind being able to set some config/formatting options for rspec later on if I ever get this working...
I made the run configuration by choosing "Rake"... I wasn't sure what to put in the "task" field so I put 'default' since it seems to match the Rakefile's last line. Any help would be great. I've been going around in circles with this for way too long! Thanks so much.
Right now my Rakefile just has :
require 'rubygems'
require 'rake'
require 'rspec/core/rake_task'
require './spec/spec_helper'
RSpec::Core::RakeTask.new do |t|
t.rspec_opts = ["-v", "-r ./spec/spec_helper.rb"]
t.pattern = 'spec/*_spec.rb'
end
task :default => [:spec]
My spec_helper just has:
require 'rubygems'
require 'rspec'
require 'simplecov'
SimpleCov.start
And an example of *_spec.rb file I have is code1_spec.rb:
require 'rubygems'
require 'rspec'
require 'spec_helper'
RSpec.describe SomeClass do
#testing to_s methods
it "returns the correct string" do
input = ["x", "{|x| x}"]
input.each { |ex|
output = SomeClass.new.run ex
expect(output.to_s).to eq(ex)
}
end
end
请先登录再写评论。
Hi,
here are some ideas about troubleshooting the problem:
Hope this helps, Oleg.
I did all 3... in reverse order :)
After I removed the t.pattern line and added (:spec) to the task call I ran it in the terminal by just using the "rake" command. And I also tried with "rake spec" both give the same results as when I'm in RubyMine...
Coverage report generated for RSpec to /Users/MyName/RubymineProjects/FunStuff/ProjectHome/coverage. 0.0 / 0.0 LOC (100.0%) covered.
Which still seems to make me think the rakefile is having trouble talking to the *_spec.rb files.
but spec_helper must be ok since it's returning the coverage report...
I got it to work ... The problem was with this line:
When I took out the "-v" and just have:
Then it works.
But... there aren't any examples of rspec_opts with multiple options set in the RSpec documentation so I don't know how to do it if I really needed to keep that "-v" in there or needed to use multiple options.
So glad I got this much working though. I had to upgrade to RubyMine 7.0 after being unable to fix deprecation warnings for my 6.3.
If anyone can show an example of how to include two elements for the rspec_opts that'd be great!
Hi,
array of strings is the right way to pass several args to rspec the problem is that "-v" forces it to just show version and exit so you shouldn't use it in rake task.
Regards, Oleg.