| 1 |
Feature: Creating a rake task |
| 2 |
|
| 3 |
RSpec ships with a rake task with a number of useful options. |
| 4 |
|
| 5 |
We recommend you wrap this in a `rescue` clause so that you can |
| 6 |
use your `Rakefile` in an environment where RSpec is unavailable |
| 7 |
(for example on a production server). e.g: |
| 8 |
|
| 9 |
```ruby |
| 10 |
begin |
| 11 |
require 'rspec/core/rake_task' |
| 12 |
RSpec::Core::RakeTask.new(:spec) |
| 13 |
rescue LoadError |
| 14 |
end |
| 15 |
``` |
| 16 |
|
| 17 |
Scenario: Default options with passing spec (prints command and exit status is 0) |
| 18 |
Given a file named "Rakefile" with: |
| 19 |
"""ruby |
| 20 |
|
| 21 |
begin |
| 22 |
require 'rspec/core/rake_task' |
| 23 |
|
| 24 |
RSpec::Core::RakeTask.new(:spec) |
| 25 |
|
| 26 |
task :default => :spec |
| 27 |
rescue LoadError |
| 28 |
# no rspec available |
| 29 |
end |
| 30 |
""" |
| 31 |
And a file named "spec/thing_spec.rb" with: |
| 32 |
"""ruby |
| 33 |
RSpec.describe "something" do |
| 34 |
it "does something" do |
| 35 |
# pass |
| 36 |
end |
| 37 |
end |
| 38 |
""" |
| 39 |
When I run `rake` |
| 40 |
Then the output should match: |
| 41 |
""" |
| 42 |
(ruby(\d\.\d(.\d)?)?|rbx) -I\S+ [\/\S]+\/exe\/rspec |
| 43 |
""" |
| 44 |
Then the exit status should be 0 |
| 45 |
|
| 46 |
Scenario: Default options with failing spec (exit status is 1) |
| 47 |
Given a file named "Rakefile" with: |
| 48 |
"""ruby |
| 49 |
begin |
| 50 |
require 'rspec/core/rake_task' |
| 51 |
|
| 52 |
RSpec::Core::RakeTask.new(:spec) |
| 53 |
|
| 54 |
task :default => :spec |
| 55 |
rescue LoadError |
| 56 |
# no rspec available |
| 57 |
end |
| 58 |
""" |
| 59 |
And a file named "spec/thing_spec.rb" with: |
| 60 |
"""ruby |
| 61 |
RSpec.describe "something" do |
| 62 |
it "does something" do |
| 63 |
fail |
| 64 |
end |
| 65 |
end |
| 66 |
""" |
| 67 |
When I run `rake` |
| 68 |
Then the exit status should be 1 |
| 69 |
|
| 70 |
Scenario: Setting `fail_on_error = false` with failing spec (exit status is 0) |
| 71 |
Given a file named "Rakefile" with: |
| 72 |
"""ruby |
| 73 |
begin |
| 74 |
require 'rspec/core/rake_task' |
| 75 |
|
| 76 |
RSpec::Core::RakeTask.new(:spec) do |t| |
| 77 |
t.fail_on_error = false |
| 78 |
end |
| 79 |
|
| 80 |
task :default => :spec |
| 81 |
rescue LoadError |
| 82 |
# no rspec available |
| 83 |
end |
| 84 |
""" |
| 85 |
And a file named "spec/thing_spec.rb" with: |
| 86 |
"""ruby |
| 87 |
RSpec.describe "something" do |
| 88 |
it "does something" do |
| 89 |
fail |
| 90 |
end |
| 91 |
end |
| 92 |
""" |
| 93 |
When I run `rake` |
| 94 |
Then the exit status should be 0 |
| 95 |
|
| 96 |
Scenario: Passing arguments to the `rspec` command using `rspec_opts` |
| 97 |
Given a file named "Rakefile" with: |
| 98 |
"""ruby |
| 99 |
begin |
| 100 |
require 'rspec/core/rake_task' |
| 101 |
|
| 102 |
RSpec::Core::RakeTask.new(:spec) do |t| |
| 103 |
t.rspec_opts = "--tag fast" |
| 104 |
end |
| 105 |
rescue LoadError |
| 106 |
# no rspec available |
| 107 |
end |
| 108 |
""" |
| 109 |
And a file named "spec/thing_spec.rb" with: |
| 110 |
"""ruby |
| 111 |
RSpec.describe "something" do |
| 112 |
it "has a tag", :fast => true do |
| 113 |
# pass |
| 114 |
end |
| 115 |
|
| 116 |
it "does not have a tag" do |
| 117 |
fail |
| 118 |
end |
| 119 |
end |
| 120 |
""" |
| 121 |
When I run `rake spec` |
| 122 |
Then the exit status should be 0 |
| 123 |
Then the output should match: |
| 124 |
""" |
| 125 |
(ruby(\d\.\d(.\d)?)?|rbx) -I\S+ [\/\S]+\/exe\/rspec --pattern spec[\/\\*{,}]+_spec.rb --tag fast |
| 126 |
""" |
| 127 |
|
| 128 |
Scenario: Passing rake task arguments to the `rspec` command via `rspec_opts` |
| 129 |
Given a file named "Rakefile" with: |
| 130 |
"""ruby |
| 131 |
begin |
| 132 |
require 'rspec/core/rake_task' |
| 133 |
|
| 134 |
RSpec::Core::RakeTask.new(:spec, :tag) do |t, task_args| |
| 135 |
t.rspec_opts = "--tag #{task_args[:tag]}" |
| 136 |
end |
| 137 |
rescue LoadError |
| 138 |
# no rspec available |
| 139 |
end |
| 140 |
""" |
| 141 |
And a file named "spec/thing_spec.rb" with: |
| 142 |
"""ruby |
| 143 |
RSpec.describe "something" do |
| 144 |
it "has a tag", :fast => true do |
| 145 |
# pass |
| 146 |
end |
| 147 |
|
| 148 |
it "does not have a tag" do |
| 149 |
fail |
| 150 |
end |
| 151 |
end |
| 152 |
""" |
| 153 |
When I run `rake spec[fast]` |
| 154 |
Then the exit status should be 0 |
| 155 |
Then the output should match: |
| 156 |
""" |
| 157 |
(ruby(\d\.\d(.\d)?)?|rbx) -I\S+ [\/\S]+\/exe\/rspec --pattern spec[\/\\*{,}]+_spec.rb --tag fast |
| 158 |
""" |