【发布时间】:2013-12-06 11:34:13
【问题描述】:
如果我运行这个命令“rspec ./spec/requests/api/v1/password_reset_request_spec.rb”,这个文件中的所有测试都会通过。
但是,当我运行“rspec”时,我在这个文件中的测试失败了。
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)>'
这是调用方法的那一行:
RunnerMailer.reset_password(@identity, @identity.reset_password_token).deliver
这是 RunnerMailer 类:
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
知道为什么当我执行 'rspec file_path' 而不是执行 'rspec' 时测试通过了吗?
编辑 1
我还有一个黄瓜功能,测试通过了。
谢谢
【问题讨论】:
-
你试过
rspec spec/requests/api/v1,如果成功的话,rspec spec/requests/api(等等)看看你是否可以缩小与之前运行的其他测试的关系? -
谢谢!事实上,是的,我在服务规范中存根这个模型,这就是它找不到方法的原因!
-
但是一个规范中的变化不应该渗透到另一个规范中。你能说更多吗?
-
是的,抱歉,事实上,对于我的服务规范,我试图避免需要 spec_helper 和 active_record 以进行更快的测试,所以在服务规范文件的顶部我有“class RunnerMailer; end”所以我猜这不是存根类的好方法。
-
所以我现在不是在文件顶部做,而是在这个服务规范的 before(:each) 中做。但我不确定这是否是最好的方法。
标签: ruby-on-rails rspec actionmailer