System notice: In light of the Debian OpenSSL security issue we've regenerated the server keys. See this thread for instructions and the new key fingerprints.

Commit a1f453766ebd3bf1193fd327c8278b9a99258607

Improved stub_model such that new_record? does "the right thing"

Commit diff

rspec/CHANGES

 
55used it, and they will now break. Just replace the metaclass call with (class << self; self; end)
66and all will be well.
77
8* Improved stub_model such that new_record? does "the right thing"
89* Patch from Pat Maddox to get integrate_views to work in nested example groups.
910* Patch from Pat Maddox to get controller_name to work in nested example groups.
1011* Patch from Corey Haines to add include_text matcher (rspec_on_rails)
toggle raw diff

rspec_on_rails/lib/spec/rails/example/rails_example_group.rb

 
5252 m
5353 end
5454
55 # Creates an instance of a +model_class+ that is prohibited
56 # from accessing the database. It comes with an id, although
57 # you can stub the id explicitly.
55 # :call-seq:
56 # stub_model(Model)
57 # stub_model(Model, hash_of_stubs)
58 #
59 # Creates an instance of +Model+ that is prohibited from accessing the
60 # database. For each key in +hash_of_stubs+, if the model has a
61 # matching attribute (determined by asking it, which it answers based
62 # on schema.rb) are simply assigned the submitted values. If the model
63 # does not have a matching attribute, the key/value pair is assigned as
64 # a stub return value using RSpec's mocking/stubbing framework.
65 #
66 # new_record? is overridden to return the result of id.nil? This means
67 # that by default new_record? will return false. If you explicitly set
68 # :id => nil, the object will behave as you would expect, and return
69 # true for new_record?
5870 #
5971 # == Examples
6072 #
6173 # stub_model(Person)
6274 # stub_model(Person, :id => 37)
6375 # stub_model(Person) do |person|
64 # person.first_name = "David"
76 # model.first_name = "David"
6577 # end
6678 def stub_model(model_class, stubs = {})
6779 id = @@model_id
8282 :id => id
8383 }.merge(stubs)
8484 returning model_class.new do |model|
85 add_stubs(model, stubs)
85 model.id = stubs.delete(:id)
8686 (class << model; self; end).class_eval do
8787 def connection
8888 raise IllegalDataAccessException.new("stubbed models are not allowed to access the database")
8989 end
90 def new_record?
91 id.nil?
92 end
9093 end
94 stubs.each do |k,v|
95 if model.has_attribute?(k)
96 model[k] = stubs.delete(k)
97 end
98 end
99 add_stubs(model, stubs)
91100 yield model if block_given?
92101 end
93102 end
94
95103 #--
96104 # TODO - Shouldn't this just be an extension of stub! ??
97105 # - object.stub!(:method => return_value, :method2 => return_value2, :etc => etc)
toggle raw diff

rspec_on_rails/spec/rails/mocks/stub_model_spec.rb

 
66 stub_model(MockableModel).id.should be >= 1000
77 end
88
9 it "should say it is not a new record by default" do
10 stub_model(MockableModel).should_not be_new_record
11 end
12
913 it "should accept a stub id" do
10 stub_model(MockableModel, :id => 3).id.should == 3
14 stub_model(MockableModel, :id => 37).id.should == 37
15 end
16
17 it "should say it is a new record when id is set to nil" do
18 stub_model(MockableModel, :id => nil).should be_new_record
1119 end
1220
1321 it "should accept a stub anything" do
1422 stub_model(MockableModel, :foo => "bar").foo.should == "bar"
1523 end
1624
25 it "should accept a stub for save" do
26 stub_model(MockableModel, :save => false).save.should be(false)
27 end
28
1729 it "should raise when hitting the db" do
1830 lambda do
1931 stub_model(MockableModel).save
toggle raw diff