| |   |
| 5 | 5 | used it, and they will now break. Just replace the metaclass call with (class << self; self; end) |
| 6 | 6 | and all will be well. |
| 7 | 7 | |
| 8 | * Added as_new_record to stub_model e.g. stub_model(Foo).as_new_record |
| 8 | 9 | * Improved stub_model such that new_record? does "the right thing" |
| 9 | 10 | * Patch from Pat Maddox to get integrate_views to work in nested example groups. |
| 10 | 11 | * Patch from Pat Maddox to get controller_name to work in nested example groups. |
| toggle raw diff |
--- a/rspec/CHANGES
+++ b/rspec/CHANGES
@@ -5,6 +5,7 @@ Note: we've removed the metaclass method from Object. There were some generated
used it, and they will now break. Just replace the metaclass call with (class << self; self; end)
and all will be well.
+* Added as_new_record to stub_model e.g. stub_model(Foo).as_new_record
* Improved stub_model such that new_record? does "the right thing"
* Patch from Pat Maddox to get integrate_views to work in nested example groups.
* Patch from Pat Maddox to get controller_name to work in nested example groups. |
| |   |
| 2 | 2 | require File.dirname(__FILE__) + '/ar_classes' |
| 3 | 3 | |
| 4 | 4 | describe "stub_model" do |
| 5 | | it "should have an id starting at 1000" do |
| 6 | | stub_model(MockableModel).id.should be >= 1000 |
| 7 | | end |
| 5 | describe "defaults" do |
| 6 | it "should have an id" do |
| 7 | stub_model(MockableModel).id.should be > 0 |
| 8 | end |
| 8 | 9 | |
| 9 | | it "should say it is not a new record by default" do |
| 10 | | stub_model(MockableModel).should_not be_new_record |
| 10 | it "should say it is not a new record" do |
| 11 | stub_model(MockableModel).should_not be_new_record |
| 12 | end |
| 11 | 13 | end |
| 12 | 14 | |
| 13 | 15 | it "should accept a stub id" do |
| … | … | |
| 20 | 20 | stub_model(MockableModel, :id => nil).should be_new_record |
| 21 | 21 | end |
| 22 | 22 | |
| 23 | | it "should accept a stub anything" do |
| 23 | it "should accept any arbitrary stub" do |
| 24 | 24 | stub_model(MockableModel, :foo => "bar").foo.should == "bar" |
| 25 | 25 | end |
| 26 | 26 | |
| … | … | |
| 28 | 28 | stub_model(MockableModel, :save => false).save.should be(false) |
| 29 | 29 | end |
| 30 | 30 | |
| 31 | describe "#as_new_record" do |
| 32 | it "should say it is a new record" do |
| 33 | stub_model(MockableModel).as_new_record.should be_new_record |
| 34 | end |
| 35 | |
| 36 | it "should have a nil id" do |
| 37 | stub_model(MockableModel).as_new_record.id.should be(nil) |
| 38 | end |
| 39 | end |
| 40 | |
| 31 | 41 | it "should raise when hitting the db" do |
| 32 | 42 | lambda do |
| 33 | 43 | stub_model(MockableModel).save |
| … | … | |
| 52 | 52 | |
| 53 | 53 | end |
| 54 | 54 | |
| 55 | | describe "stub_model as association", :type => :view do |
| 55 | describe "stub_model as association" do |
| 56 | 56 | before(:each) do |
| 57 | 57 | @real = AssociatedModel.create! |
| 58 | 58 | @stub_model = stub_model(MockableModel) |
| … | … | |
| 68 | 68 | end |
| 69 | 69 | end |
| 70 | 70 | |
| 71 | | describe "stub_model with a block", :type => :view do |
| 72 | | it "should yield the stub" do |
| 73 | | model = stub_model(MockableModel) do |model| |
| 74 | | model.stub!(:foo).and_return(:bar) |
| 71 | describe "stub_model with a block" do |
| 72 | it "should yield the model" do |
| 73 | model = stub_model(MockableModel) do |block_arg| |
| 74 | @block_arg = block_arg |
| 75 | 75 | end |
| 76 | | model.foo.should == :bar |
| 76 | model.should be(@block_arg) |
| 77 | 77 | end |
| 78 | 78 | end |
| toggle raw diff |
--- a/rspec_on_rails/spec/rails/mocks/stub_model_spec.rb
+++ b/rspec_on_rails/spec/rails/mocks/stub_model_spec.rb
@@ -2,12 +2,14 @@ require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/ar_classes'
describe "stub_model" do
- it "should have an id starting at 1000" do
- stub_model(MockableModel).id.should be >= 1000
- end
+ describe "defaults" do
+ it "should have an id" do
+ stub_model(MockableModel).id.should be > 0
+ end
- it "should say it is not a new record by default" do
- stub_model(MockableModel).should_not be_new_record
+ it "should say it is not a new record" do
+ stub_model(MockableModel).should_not be_new_record
+ end
end
it "should accept a stub id" do
@@ -18,7 +20,7 @@ describe "stub_model" do
stub_model(MockableModel, :id => nil).should be_new_record
end
- it "should accept a stub anything" do
+ it "should accept any arbitrary stub" do
stub_model(MockableModel, :foo => "bar").foo.should == "bar"
end
@@ -26,6 +28,16 @@ describe "stub_model" do
stub_model(MockableModel, :save => false).save.should be(false)
end
+ describe "#as_new_record" do
+ it "should say it is a new record" do
+ stub_model(MockableModel).as_new_record.should be_new_record
+ end
+
+ it "should have a nil id" do
+ stub_model(MockableModel).as_new_record.id.should be(nil)
+ end
+ end
+
it "should raise when hitting the db" do
lambda do
stub_model(MockableModel).save
@@ -40,7 +52,7 @@ describe "stub_model" do
end
-describe "stub_model as association", :type => :view do
+describe "stub_model as association" do
before(:each) do
@real = AssociatedModel.create!
@stub_model = stub_model(MockableModel)
@@ -56,11 +68,11 @@ describe "stub_model as association", :type => :view do
end
end
-describe "stub_model with a block", :type => :view do
- it "should yield the stub" do
- model = stub_model(MockableModel) do |model|
- model.stub!(:foo).and_return(:bar)
+describe "stub_model with a block" do
+ it "should yield the model" do
+ model = stub_model(MockableModel) do |block_arg|
+ @block_arg = block_arg
end
- model.foo.should == :bar
+ model.should be(@block_arg)
end
end |