Hello. I have this unit test: test "User validations" do user = User.new assert user.invalid? assert user.errors[:uid].any? assert user.errors[:name].any? assert user.errors[:role].any? end When I run it I have this error: null value in column "uid" violates not-null constraint: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES (''2012-11-18 19:37:39'', ''2012-11-18 19:37:39'', 298486374). In the fixture user I''ve put: one: uid: "xxx.xxx" Why the unit test got the error? Thank you. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 18 November 2012 19:42, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello. > I have this unit test: > > test "User validations" do > user = User.new > assert user.invalid? > assert user.errors[:uid].any? > assert user.errors[:name].any? > assert user.errors[:role].any? > end > > When I run it I have this error: > > null value in column "uid" violates not-null constraint: INSERT INTO > "users" ("created_at", "updated_at", "id") VALUES (''2012-11-18 > 19:37:39'', ''2012-11-18 19:37:39'', 298486374). > In the fixture user I''ve put: > > one: > uid: "xxx.xxx" > > Why the unit test got the error?Is it only that test that gives the problem or does it happen when you run any test? If it was a problem with the fixture then it will happen whatever test you run, I think. I don''t think that it is necessary to include quotes in the fixture file but I would not expect that to cause the problem. What happens if you empty the users fixtures file? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Jordon Bedwell
2012-Nov-18 21:24 UTC
Re: rake test:units error because not null constraint.
On Sun, Nov 18, 2012 at 1:42 PM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello. > I have this unit test: > > test "User validations" do > user = User.new > assert user.invalid? > assert user.errors[:uid].any? > assert user.errors[:name].any? > assert user.errors[:role].any? > endYou should probably move each of those assertions to a test of their own using contexts. describe User do context "Validations" it "should have an error on blah" end end But you can make those kind of tests mad easy by just using should matchers then it comes down to something so simple you never forget to properly do it: describe User do it { should validate_presence_of(:name) } end> > When I run it I have this error: > > null value in column "uid" violates not-null constraint: INSERT INTO > "users" ("created_at", "updated_at", "id") VALUES (''2012-11-18 > 19:37:39'', ''2012-11-18 19:37:39'', 298486374). > In the fixture user I''ve put: > > one: > uid: "xxx.xxx"If I remember right you should be doing users(:one) instead of User.new but I''m not so sure on that subject 100% because I haven''t played with Fixtures since the day after I started learning Rails and discovered factory_girl. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 18 November 2012 21:24, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 November 2012 19:42, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hello. >> I have this unit test: >> >> test "User validations" do >> user = User.new >> assert user.invalid? >> assert user.errors[:uid].any? >> assert user.errors[:name].any? >> assert user.errors[:role].any? >> end >> >> When I run it I have this error: >> >> null value in column "uid" violates not-null constraint: INSERT INTO >> "users" ("created_at", "updated_at", "id") VALUES (''2012-11-18 >> 19:37:39'', ''2012-11-18 19:37:39'', 298486374). >> In the fixture user I''ve put: >> >> one: >> uid: "xxx.xxx" >> >> Why the unit test got the error? > > Is it only that test that gives the problem or does it happen when you > run any test? If it was a problem with the fixture then it will > happen whatever test you run, I think. > > I don''t think that it is necessary to include quotes in the fixture > file but I would not expect that to cause the problem. > > What happens if you empty the users fixtures file?same thing. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.