Machinist generates NOT unique data on Rspec

226 views
Skip to first unread message

Haruka YAGNI

unread,
Mar 8, 2011, 2:21:16 AM3/8/11
to Machinist Users
I wrote a test for Comment model on rspec, which is named
"comment_spec.rb".

ruby -S bundle exec rspec spec/models/comment_spec.rb
works well at first.

But
rake spec
does not pass, and I get the error on creation of new User
SQLite3::ConstraintException: column identity_url is not unique.

Then
ruby -S bundle exec rspec spec/models/comment_spec.rb
comes to fail, unless db/test.sqlite3 is deleted and re-generated.

Calling "Comment.make" occurs this error, and identity_url is unique
member of User model with index (this is url for OpenID auth).

Please let me know how to pass the test.
Thank you in advance.

Versions:
Ruby 1.8.7
Rails 3.0.4
Rspec core:2.5.1 rails:2.5.0
Machinist 1.0.6

Here is part of blueprint.rb

Sham.define do
name { Faker::Name.first_name }
introduction { Faker::Lorem.paragraphs(3).join("\n\n") }
fullname { Faker::Name.name }
url(:unique=>true) { "http://example.com/id/" + rand(1000).to_s }
point(:unique=>false) { rand(100) }

title { Faker::Company.name }
asin { (1..10).map{('0'..'9').to_a.sort_by{|item| rand}[1]}.join }

stars(:unique=>false) { rand(50)/10.0 }
status(:unique=>false) { rand(2) == 0 ? 'read' : 'reading' }
end

User.blueprint do
point { Sham.point }
introduction { Sham.introduction }
name { Sham.name }
fullname { Sham.fullname }
identity_url { Sham.url }
end

Comment.blueprint() do
book
user
point { Sham.point }
stars { Sham.stars }
title { Sham.title }
body { Sham.introduction }
end

-----------

Full error here (the same error occurs 7 times)
7) Comment with parent and children should be accessable via find
Failure/Error: @child = Comment.make(:children)
ActiveRecord::RecordNotUnique:
SQLite3::ConstraintException: column identity_url is not
unique: INSERT INTO "users" ("disabled", "sign_in_count", "email",
"unlock_token", "identity_url", "current_sign_in_at",
"authentication_token", "confirmed_at", "last_sign_in_ip", "name",
"introduction", "remember_token", "point", "created_at",
"remember_created_at", "current_sign_in_ip", "confirmation_sent_at",
"failed_attempts", "recommended_book_size", "last_sign_in_at",
"updated_at", "wished_book_size", "confirmation_token", "fullname",
"read_book_size", "locked_at") VALUES ('f', 0, NULL, NULL, 'http://
example.com/id/37', NULL, NULL, NULL, NULL, 'Fern', 'Quam totam
corporis similique voluptatibus quaerat laboriosam. Eveniet possimus
asperiores voluptatibus recusandae non quod eum. Quo odio et
voluptatem id voluptatem. Et voluptatem officia voluptatem omnis
voluptas.

Nobis iste nostrum beatae corrupti ea qui debitis. Laborum
exercitationem architecto ea et eum. Voluptate velit cumque dolorum
quia provident. Omnis ea a id. Consequatur aut est hic.

Quas temporibus iusto quidem non repellat. Quia deserunt est
doloribus tempora. Vel dolores eos ad aliquam. Dolorum voluptatem
eveniet cum sit velit quo et fugiat. Dolores et et quibusdam repellat
adipisci eligendi qui.', NULL, 37, '2011-03-08 07:11:33.657230', NULL,
NULL, NULL, 0, 10, NULL, '2011-03-08 07:11:33.657230', 10, NULL, 'Aric
Smith', 10, NULL)
# ./spec/blueprints.rb:63
# ./spec/models/comment_spec.rb:42

Pete Yandell

unread,
Mar 8, 2011, 6:27:22 AM3/8/11
to Machinist Users
Sounds like your database isn't getting cleaned out between specs. Do
you have transactional fixtures turned on? What does your actual spec
look like?

Haruka YAGNI

unread,
Mar 8, 2011, 6:54:58 AM3/8/11
to machini...@googlegroups.com
Pete, thank you for your reply.

transactional fixtures are enabled.
In spec/spec_helper.rb
config.use_transactional_fixtures = true
config.before(:all) { Sham.reset(:before_all) }
config.before(:each) { Sham.reset(:before_each) }


The following is the whole of comment_spec.rb, spec for comment model.

I have little knowledge about Rspec, Machinist and Testing. My code
must be strange, or crasy.
I would be happy, if you point out unordinal codes.

------------ comment_spec.rb -----------------
require File.dirname(__FILE__) + '/../spec_helper'

describe Comment, "with wrong fields" do
it "should not be valid" do
c = Comment.new
c.should_not be_valid
[:stars, :title, :body].each do |sym|
c.errors[sym].any?.should be_true
end
end

it "might have negative stars" do
c = Comment.make
c.stars = -1
c.should_not be_valid
end

it "might have too many stars" do
c = Comment.make
c.stars = 8
c.should_not be_valid
end
end

describe Comment, "with right fields" do
before do
@c = Comment.make
end

it "should be valid" do
@c.should be_valid
end

it "should have an book and user" do
@c.user.should be_instance_of User
@c.book.should be_instance_of Book
end
end

describe Comment, "with parent and children" do
before(:all) do
@child = Comment.make(:children)
end

it "should have the same book" do
@child.should be_valid

@child.book = Book.make
@child.should_not be_valid
end

it "should be exisiting parent" do
Comment.find(:all, :conditions => ['parent_comment IS
NULL']).count.should == 1
end

it "should be accessible via find" do
Comment.find(:first, :conditions => ['parent_comment == ?',
@child.parent_comment.id]).id.should == @child.id
end
end
------------ comment_spec.rb -----------------


--
Haruka YAGNI

Pete Yandell

unread,
Mar 8, 2011, 7:08:43 AM3/8/11
to Machinist Users
It's because you're doing a before(:all). RSpec before(:all) blocks
run outside of transactional fixtures, mock and stub cleanup, and all
sorts of other good things you want in your tests. The general rule is
that you should never use them.

Haruka YAGNI

unread,
Mar 8, 2011, 8:12:14 AM3/8/11
to machini...@googlegroups.com
Thank you!!
I deleted (:all) from every spec, and all errors have gone away!


--
Haruka YAGNI

Reply all
Reply to author
Forward
0 new messages