问题
If I run this command "rspec ./spec/requests/api/v1/password_reset_request_spec.rb" all the test inside this file pass.
However when I run "rspec" I've a faillure on the tests inside this file.
1) /api/v1/password_reset #request when the email match with a runner when there is no request pending create a token for reset the password
Failure/Error: post("/api/v1/password_reset/request", @params)
NoMethodError:
undefined method `reset_password' for RunnerMailer:Class
# ./app/services/password_manager.rb:35:in `reset_password'
# ./app/controllers/api/v1/password_reset_controller.rb:31:in `request_new_password'
# ./spec/requests/api/v1/password_reset_request_spec.rb:108:in `block (5 levels) in <top (required)>'
This is the line where the the method is called:
RunnerMailer.reset_password(@identity, @identity.reset_password_token).deliver
And this is the RunnerMailer class:
class RunnerMailer < ActionMailer::Base
default from: "no-reply@goodgym.org"
def reset_password(runner, token)
@link = "http://url/password_reset/request/" + token
mail(to: runner.email, subject: "Goodgym -- Reset your password")
end
end
Any idea why the test pass when I do 'rspec file_path' and not when I do 'rspec' ?
EDIT 1
I've also a cucumber feature for that and the test pass.
Thanks
回答1:
When the execution of an individual spec succeeds but the same spec fails when run as part of the larger suite, it indicates that the prior execution of the other tests is affecting the result.
If the spec in question is deeply nested, as in this case, one way to isolate the problem is to run all the specs in successive directories up from the spec in question until you cause a failure. Once you hit the directory that causes the problem, then you can isolate further by specifying different series of specs to run prior to the failing test until you isolate the problematic spec.
For example, in this case, you would run rspec spec/requests/api/v1
and if that succeeds, rspec spec/requests/api
, and if that succeeds rspec spec/requests
.
Since under normal conditions RSpec is careful to rollback any changes that individual tests make (both to the Ruby runtime and the database), interference is usually due to some code being run outside of the normal RSpec framework. In general, all test code should be included within the describe
blocks.
回答2:
For me I had
config.before(:all) do
Rails.application.load_seed # loading seeds
end
in my rails_helper.rb
file which was loading my seeds at the beginning of each file which was causing conflicts.
I changed my code to load the seeds at the beginning of the suite.
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
Rails.application.load_seed # loading seeds
end
来源:https://stackoverflow.com/questions/20422856/rpsec-tests-failed-with-rspec-but-not-with-rspec-path-of-the-file