如何使用 RSpec 测试我的设备用户模型验证?
我可以在 RSpec 中找到有关测试设备用户控制器和视图的建议。我还看到有人建议 devise gem 代码已经过测试,因此花费大量时间重新发明轮子是没有用的。
但是,我的用户模型还有其他字段需要在用户注册时进行验证。我在 user.rb 模型中使用标准 validates...
语句。例如:
validates_presence_of :nickname
我尝试在 user_spec.rb 中使用简单的验证测试,但是当我尝试像这样创建用户时:
record = Factory.create(:user)
我收到此错误:
undefined method `encode!' for "Confirmation":String
encode!
方法不是来自我的代码,它一定是 devise 正在使用的宝石之一,但我还没有找到它。
我尝试使用 User.new 和 Factory Girl 创建用户。无论哪种方式我都会遇到同样的错误。这个规范一直在通过,直到我更新了我的所有宝石。不幸的是我没有记录当时更新的所有内容。我已尝试将设备回滚到以前的版本,但仍然遇到相同的错误。
Rails 3、RSpec2
感谢您的建议。
I can find recommendations for testing devise user controllers and views in RSpec. I've also seen suggestions that the devise gem code is already tested so it's not useful to spend a lot of time reinventing the wheel.
However, my user model has other fields that I need validated when the user signs up. I'm using standard validates...
statements in the user.rb model. For example:
validates_presence_of :nickname
I'm trying to use simple validation testing in my user_spec.rb, but when I try to create the user like this:
record = Factory.create(:user)
I get this error:
undefined method `encode!' for "Confirmation":String
The encode!
method is not coming from my code, it must be one of the gems that devise is using, but I haven't been able to find it, yet.
I've tried creating the user using User.new and Factory Girl. I get the same error either way. This spec was passing until I did an update of all my gems. Unfortunately I didn't keep a note of everything that got updated at the time. I've tried rolling devise back to previous versions but still get the same error.
Rails 3, RSpec2
Thanks for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来不错,也许是,我的测试代码可以帮助你:
user_spec.rb
user.rb (Model)
It seems to be fine, may be, my testing code helps you out:
user_spec.rb
user.rb (Model)