spec/mongoid/token_spec.rb in mongoid_token-1.0.0 vs spec/mongoid/token_spec.rb in mongoid_token-1.1.0
- old
+ new
@@ -82,11 +82,11 @@
it "should only generate unique tokens" do
Link.create_indexes
1000.times do
@link = Link.create(:url => "http://involved.com.au")
- Link.count(:conditions => {:token => @link.token}).should == 1
+ Link.where(:token => @link.token).count.should == 1
end
end
it "should have a token containing only the specified characters" do
50.times do
@@ -148,36 +148,41 @@
it "should fail with an exception after 3 retries (by default)" do
Link.destroy_all
Link.create_indexes
@first_link = Link.create(:url => "http://involved.com.au")
+ Link.any_instance.stub(:generate_token).and_return(@first_link.token)
@link = Link.new(:url => "http://fail.com")
- def @link.create_token(l,c) # override to always generate a duplicate
- super
- self.token = Link.first.token
- end
- lambda{ @link.save }.should raise_error(Mongoid::Token::CollisionRetriesExceeded)
+ #lambda{ @link.save! }.should raise_error(Mongoid::Token::CollisionRetriesExceeded)
+ expect { @link.save! }.to raise_error(Mongoid::Token::CollisionRetriesExceeded)
+
Link.count.should == 1
Link.where(:token => @first_link.token).count.should == 1
end
+ it "tries to resolve collisions when instantiated with create!" do
+ link = Link.create!(url: "http://example.com/1")
+
+ Link.any_instance.stub(:generate_token).and_return(link.token)
+
+ expect { Link.create!(url: "http://example.com/2") }
+ .to raise_error(Mongoid::Token::CollisionRetriesExceeded)
+ end
+
it "should not raise a custom exception if retries are set to zero" do
FailLink.destroy_all
FailLink.create_indexes
@first_link = FailLink.create(:url => "http://involved.com.au")
+ Link.any_instance.stub(:generate_token).and_return(@first_link.token)
@link = FailLink.new(:url => "http://fail.com")
- def @link.create_token(l,c) # override to always generate a duplicate
- super
- self.token = FailLink.first.token
- end
lambda{ @link.save }.should_not raise_error(Mongoid::Token::CollisionRetriesExceeded)
end
it "should create unique indexes on embedded documents" do
- @cluster = Cluster.new(:name => "CLUSTER_001")
+ @cluster = Cluster.create(:name => "CLUSTER_001")
5.times do |index|
@cluster.nodes.create!(:name => "NODE_#{index.to_s.rjust(3, '0')}")
end
@cluster.nodes.each do |node|