Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added more tests (tests for new_<modelname>! which save!s instantiate…

…d tests to the database)
  • Loading branch information...
commit 655581672d8e4b994be802528fe56fe0935a8fba 1 parent caf2698
@lawrencepit lawrencepit authored
View
26 spec/model_stubbing_spec.rb
@@ -24,9 +24,29 @@ module ModelStubbing
end
it "retrieves instantiated new records based on stubs" do
- record = new_model_stubbing_user(:default)
- record.id.should be_nil
- record.should be_new_record
+ record1 = new_model_stubbing_user(:default)
+ record1.id.should be_nil
+ record1.should be_new_record
+
+ record2 = new_model_stubbing_user
+ record2.id.should be_nil
+ record2.should be_new_record
+
+ record1.object_id.should_not == record2.object_id
+ end
+
+ it "saves instantiated new records based on stubs" do
+ record = new_model_stubbing_user!
+ record.id.should_not be_nil
+ record.should_not be_new_record
+ record.name.should == 'bob'
+ end
+
+ it "saves instantiated new records based on stubs with custom attributes" do
+ record = new_model_stubbing_user!(:name => 'jane')
+ record.id.should_not be_nil
+ record.should_not be_new_record
+ record.name.should == 'jane'
end
it "generates custom stubs" do
View
26 spec/model_stubbing_test.rb
@@ -25,9 +25,29 @@ def test_should_retrieve_instantiated_stubs
end
def test_should_retrieve_instantiated_new_records_based_on_stubs
- record = new_model_stubbing_user(:default)
- assert_nil record.id
- assert record.new_record?
+ record1 = new_model_stubbing_user(:default)
+ assert_nil record1.id
+ assert record1.new_record?
+
+ record2 = new_model_stubbing_user
+ assert_nil record2.id
+ assert record2.new_record?
+
+ assert_not_equal record1.object_id, record2.object_id
+ end
+
+ def test_should_save_instantiated_new_records_based_on_stubs
+ record = new_model_stubbing_user!
+ assert !record.id.nil?
+ assert !record.new_record?
+ assert_equal 'bob', record.name
+ end
+
+ def test_should_save_instantiated_new_records_based_on_stubs_with_custom_attributes
+ record = new_model_stubbing_user!(:name => 'jane')
+ assert !record.id.nil?
+ assert !record.new_record?
+ assert_equal 'jane', record.name
end
def test_should_generate_custom_stubs
View
26 spec/model_stubbing_test_spec.rb
@@ -25,9 +25,29 @@ def test_should_retrieve_instantiated_stubs
end
def test_should_retrieve_instantiated_new_records_based_on_stubs
- record = new_model_stubbing_user(:default)
- assert_nil record.id
- assert record.new_record?
+ record1 = new_model_stubbing_user(:default)
+ assert_nil record1.id
+ assert record1.new_record?
+
+ record2 = new_model_stubbing_user
+ assert_nil record2.id
+ assert record2.new_record?
+
+ assert_not_equal record1.object_id, record2.object_id
+ end
+
+ def test_should_save_instantiated_new_records_based_on_stubs
+ record = new_model_stubbing_user!
+ assert !record.id.nil?
+ assert !record.new_record?
+ assert_equal 'bob', record.name
+ end
+
+ def test_should_save_instantiated_new_records_based_on_stubs_with_custom_attributes
+ record = new_model_stubbing_user!(:name => 'jane')
+ assert !record.id.nil?
+ assert !record.new_record?
+ assert_equal 'jane', record.name
end
def test_should_generate_custom_stubs
View
11 spec/models.rb
@@ -55,6 +55,12 @@ def inspect
"#{self.class.name} ##{id} => #{@attributes.inspect}"
end
+ def save
+ @new_record = false
+ self.id = db_id if self.id.nil?
+ end
+ alias :save! :save
+
def method_missing(name, *args)
if name.to_s =~ /(\w+)=$/
set_attribute($1, args[0])
@@ -73,6 +79,11 @@ def set_attribute(key, value)
send "#{key}=", value
attributes[key] = value
end
+
+ @@db_id = 0
+ def db_id
+ @@db_id += 1
+ end
end
User = Class.new BlankModel
Please sign in to comment.
Something went wrong with that request. Please try again.