Autotest 和 Factory Girl 的验证问题
我的自动测试有问题。在我的用户模型中是用户名和电子邮件地址是唯一的。当我开始自动测试时,一切正常。在第二轮中,从自动测试中,我有一个
验证失败:电子邮件已被占用,用户名已被占用
仅在两次测试中出现错误。我不明白为什么,我使用带有序列的factorygirl,并且每次都会生成新的用户名。
这是我的 rspec2 文件:
describe User do
specify { Factory.build(:user).should be_valid }
context "unique values" do
before :all do
@user = Factory.create(:user)
end
it "should have an unique email address" do
Factory.build(:user, :email => @user.email).should_not be_valid
end
it "should have an unique username" do
Factory.build(:user, :username => @user.username).should_not be_valid
end
end
context "required attributes" do
it "should be invalid without an email address" do
Factory.build(:user, :email => nil).should_not be_valid
end
it "should be invalid without an username" do
Factory.build(:user, :username => nil).should_not be_valid
end
it "should be invalid without an password" do
Factory.build(:user, :password => nil).should_not be_valid
end
it "should be invalid without an address" do
Factory.build(:user, :address => nil).should_not be_valid
end
end
end
工厂:
Factory.sequence :username do |n|
"Outfit#{n}er"
end
Factory.sequence :email do |n|
"user#{n}@example.com"
end
Factory.define :user do |u|
u.username { Factory.next :username }
u.email { Factory.next :email }
u.password 'secret'
u.phone_number '02214565854'
u.association :address, :factory => :address
end
Factory.define :confirmed_user, :parent => :user do |u|
u.after_build { |user| user.confirm! }
end
唯一两个不起作用的测试是在“唯一值”上下文中。所有其他测试均有效,没有错误。
谢谢
i have a problem with Autotest. In my user model are the username & email address unique. When i start Autotest everything works fine. In the secound round, from autotest, i have a
Validation failed: Email has already been taken, Username has already been taken
error in only two test. I dont understand why, i use factorygirl with an sequenze and this should generate everytime a new username.
This is my rspec2 file:
describe User do
specify { Factory.build(:user).should be_valid }
context "unique values" do
before :all do
@user = Factory.create(:user)
end
it "should have an unique email address" do
Factory.build(:user, :email => @user.email).should_not be_valid
end
it "should have an unique username" do
Factory.build(:user, :username => @user.username).should_not be_valid
end
end
context "required attributes" do
it "should be invalid without an email address" do
Factory.build(:user, :email => nil).should_not be_valid
end
it "should be invalid without an username" do
Factory.build(:user, :username => nil).should_not be_valid
end
it "should be invalid without an password" do
Factory.build(:user, :password => nil).should_not be_valid
end
it "should be invalid without an address" do
Factory.build(:user, :address => nil).should_not be_valid
end
end
end
The Factory:
Factory.sequence :username do |n|
"Outfit#{n}er"
end
Factory.sequence :email do |n|
"user#{n}@example.com"
end
Factory.define :user do |u|
u.username { Factory.next :username }
u.email { Factory.next :email }
u.password 'secret'
u.phone_number '02214565854'
u.association :address, :factory => :address
end
Factory.define :confirmed_user, :parent => :user do |u|
u.after_build { |user| user.confirm! }
end
The only two test that are not working are in the "unique values" context. All other test works without an error.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这正是我刚刚遇到的问题——并且有一次奇怪的经历,如果我只是运行“rake spec”,这个问题似乎不会发生。
在没有找到明显的答案后,我确实尝试了一些似乎已经解决了问题的方法!如果您使用 FactoryGirl,则似乎在 before() 块内设置类变量是罪魁祸首。
我有一些非常相似的东西:
然后:
自动测试会发现唯一性约束问题。我将 rspect 测试更改为:
......并且唯一性约束问题消失了。不确定为什么会发生这种情况,而且似乎它不应该发生,但可能是一个可行的解决方案。
This is exactly the problem I had just run into -- and had a strange experience where this problem didn't seem to happen if I was just running "rake spec".
After not finding an obvious answer, I did try something that seems to have solved the problem! It seems like setting a class variable inside a before() block is the culprit, if you're using FactoryGirl.
I had something very similar:
then:
autotest would find uniqueness constraint problems. I changed the rspect test to this:
...and the uniqueness constraint issues went away. Not sure why this happens, and it seems like it shouldn't happen, but may be a workable resolution.
经过一些测试后,我遇到了这个问题,因为数据库没有被正确清除。
如果你运行:
耙数据库:测试:准备
自动测试之前,还会出现这种情况吗?
我最终通过隔离哪些测试未被清除并修复它们(或删除它们)来解决这个问题。抱歉,我不记得出了什么问题,但有一些错误导致了它。
哦,我在没有 spork 的情况下运行它,这样我就可以看到我的记录器消息与结果混合在一起。
希望这有帮助。
I have gotten that problem from having the database not being properly cleared after some tests.
If you run:
rake db:test:prepare
before the autotest, does it still happen?
I eventually got around it by isolating which tests were not being cleared and fixing them (or removing them). Sorry, I can't remember what was the problem, but it had some error causing it.
Oh, and I ran it without spork, so that I could see my logger messages mixed in with the results.
Hope this helps.
问题可能出在您的配置文件中。您的规范配置文件(可能是spec_helper.rb)中有一行内容如下:
这应该是:
或者如果您不想使用事务性固定装置,则在测试中应该有一个 before(:each) 函数:
或者,我在我的工厂中使用Sham gem,因为它与非事务性装置和序列更兼容。
The problem is probably in your config file. You have a line in your spec config file (probably spec_helper.rb) that reads:
This should be:
Or if you don't want to use transactional fixtures, you should have a before(:each) function in your test:
Alternatively, I use the Sham gem for my factories because it's more compatible with non-transactional fixtures and sequences.
我遇到了一个非常相似的问题,我尝试了该线程上的一些建议,但没有成功。不过,我确实通过将以下内容添加到我的 spec_helper.rb 中来修复它:
另外不要忘记将 DatabaseCleaner gem 添加到您的 gemfile 中
I ran into a very similar problem and I tried several of the suggestions on this thread with no luck. I did however fix it with adding the following to my spec_helper.rb:
Also don't forget to add the DatabaseCleaner gem to your gemfile