ruby on Rails 教程 TDD“电子邮件已被占用”
我一直在阅读 Michael Hartl 的教程 http://railstutorial.org/,大部分内容都是巨大的帮助开始使用 Rails。这本书非常关注 TDD,这很好,因为我想学习 TDD,但问题是我 90% 的测试失败,并出现错误“电子邮件已被占用”。我认为发生的情况是,当测试运行时,它会创建一个电子邮件用户“[email protected]”,正如书中所建议的那样。问题是当第二个测试运行需要创建用户时,它使用相同的“ [电子邮件受保护]”电子邮件地址。我知道我见过使用工厂女孩创建一系列电子邮件地址的解决方法,但我不必这样做才能使教程中的示例正常工作。
还有其他人遇到过这个问题吗?从缺乏关于这个特定主题的问题来看,我认为这是我的代码中的一个错误,但也许其他人遇到过这个。
I have been going through Michael Hartl's tutorial http://railstutorial.org/ and for the most part it has been a huge help in getting started with Rails. The book is very focused on TDD, which is great because I wanted to learn TDD, but the problem is 90% of my tests fail with the error "Email has already been taken". I think what is happening is that when the test runs it creates a user with email "[email protected]" as suggested in the book. The problem is when the second test runs which needs to create a user, it is using the same "[email protected]" email address. I know there are workarounds I've seen using factory girl to create a sequence of email addresses but I shouldn't have to do this to get the example from the tutorial working properly.
Has anyone else run into this problem? Judging by the lack of questions on this particular topic I am thinking it is a bug in my code but maybe someone else encountered this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为您的测试清理数据库的有用资产:
https://github.com/bmabey/database_cleaner
An useful asset to clean your db for yours tests:
https://github.com/bmabey/database_cleaner
您应该搜索有关如何在每次测试后清理数据库的主题。问题也可能是由您从固定装置加载的默认用户引起的,然后当您再次在测试中创建 hin 时,但如果您说 90%,则似乎是第一种情况。
我不附加链接,因为当您使用 rspec、test::unit 或 cucumber 时,它们会有所不同,它们每个都有其优点。
快乐编码!
You should search on topic to how to clean database after each test. Issue may be as well caused by default user you load from fixtures and then when you create hin in the test again, but if you say 90% it seems like the first case.
I do not attach links because it makes a difference when you use rspec, test::unit or cucumber, each of them has its perks.
Happy coding!
我最近也遇到了这个问题。测试后应该清理测试数据库,但由于某种原因没有清理。我所做的就是运行“rake db:reset”来重置所有内容,然后确保我正在使用工厂(Factory Girl)。只需尝试使用固定装置或工厂,这样您就可以确定您实际上没有访问数据库。
I've had this problem recently as well. The test DB should be cleaned after tests but for some reason it wasn't. All I did was run 'rake db:reset' to reset everything and then make sure that I was using factories (Factory Girl). Just try using either fixtures or factories so you know for sure you aren't actually hitting the database.
我在第 11 章快结束时看到了类似的行为。我的 spec_helper.rb 是正确的。这为我解决了这个问题:
重新启动“rails s”
重新启动自动测试
耙数据库:重置
耙数据库:迁移
耙数据库:测试:准备
rake db:填充
HTH,
佩里
I saw similar behavior near the end of CH 11. My spec_helper.rb was correct. This fixed it for me:
restarted "rails s"
restarted autotest
rake db:reset
rake db:migrate
rake db:test:prepare
rake db:populate
HTH,
Perry
哎哟!问题是注释行:
在spec_helper.rb中!
新手错误。
Doh! The problem was a commented line:
in spec_helper.rb!
Newbie mistake.