spec/mongoid/token_spec.rb in mongoid_token-2.0.2 vs spec/mongoid/token_spec.rb in mongoid_token-2.1.0

- old
+ new

@@ -1,9 +1,14 @@ require File.join(File.dirname(__FILE__), %w[.. spec_helper]) describe Mongoid::Token do let(:document_class) do + Object.send(:remove_const, :Document) + class Document + include Mongoid::Document + include Mongoid::Token + end Class.new(Document) end let(:document) do document_class.create @@ -15,11 +20,14 @@ it "should be created" do expect(document).to have_field(:token) end it "should be indexed" do - expect(document).to have_index_for(:token => 1).with_options(:unique => true) + index = document.index_specifications.first + expect(index.fields).to eq([:token]) + expect(index.options).to have_key(:unique) + expect(index.options).to have_key(:sparse) end end describe "options" do it "should accept custom field names" do @@ -152,10 +160,17 @@ document.save expect(document.token).to_not be_nil expect(document.token).to_not eq token_before end end + context "when the document is initialized with a token" do + it "should not change the token after being saved" do + document_class.send(:token) + token = 'test token' + expect(document_class.create!(token: token).token).to eq token + end + end end context "when the document is cloned" do it "should set the token to nil" do document.class.send(:token, :length => 64, :contains => :alpha_upper) d2 = document.clone @@ -208,9 +223,10 @@ expect{document_class.create!}.to raise_exception(Mongoid::Token::CollisionRetriesExceeded) end end it "should not raise a custom error if another error is thrown during saving" do + I18n.enforce_available_locales = false # Supress warnings in this example document_class.send(:field, :name) document_class.send(:validates_presence_of, :name) document_class.any_instance.stub(:generate_token).and_return("1234") document_class.stub(:model_name).and_return(ActiveModel::Name.new(document_class, nil, "temp")) expect{document_class.create!}.to raise_exception(Mongoid::Errors::Validations)