David Green
2007-Jun-07 09:54 UTC
[rspec-users] checking associated objects have been deleted
I have the following model: class Book < ActiveRecord::Base has_many :taggings, :dependent => :delete_all has_many :tags, :through => :taggings end is it possible to check that associated taggings are being destroyed using mocks? I don''t want to test that rails is deleting the associations, I want to test that I have specified the association as a dependent one. the only way I can think of is to use fixtures (or create real associations in the before callback) and then check that they have been deleted using Taggings.count(). Can it be done using mocks instead? describe Book do before do @book = mock_model(Book) @tagging = mock_model(Tagging) @book.stub!(:taggings).and_return([@tagging, @tagging]) end it "should delete associated taggings when destroyed" do end end thanks dave
David Chelimsky
2007-Jun-07 12:41 UTC
[rspec-users] checking associated objects have been deleted
On 6/7/07, David Green <pocketsized at tiscali.co.uk> wrote:> I have the following model: > > class Book < ActiveRecord::Base > has_many :taggings, :dependent => :delete_all > has_many :tags, :through => :taggings > end > > is it possible to check that associated taggings are being destroyed > using mocks? I don''t want to test that rails is deleting the > associations, I want to test that I have specified the association as > a dependent one. > > the only way I can think of is to use fixtures (or create real > associations in the before callback) and then check that they have > been deleted using Taggings.count(). Can it be done using mocks > instead? > > describe Book do > > before do > @book = mock_model(Book) > @tagging = mock_model(Tagging) > @book.stub!(:taggings).and_return([@tagging, @tagging]) > end > > it "should delete associated taggings when destroyed" do > end > > endYou have to create a real book, but you can create a mock tagging: book = Book.create(:title => "the book") book.taggings << tagging = mock_model(Tagging, :[]= => true, :save => true) tagging.should_receive(:destroy) book.destroy Cheers, David> > thanks > dave > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2007-Jun-07 12:43 UTC
[rspec-users] checking associated objects have been deleted
On 6/7/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 6/7/07, David Green <pocketsized at tiscali.co.uk> wrote: > > I have the following model: > > > > class Book < ActiveRecord::Base > > has_many :taggings, :dependent => :delete_all > > has_many :tags, :through => :taggings > > end > > > > is it possible to check that associated taggings are being destroyed > > using mocks? I don''t want to test that rails is deleting the > > associations, I want to test that I have specified the association as > > a dependent one. > > > > the only way I can think of is to use fixtures (or create real > > associations in the before callback) and then check that they have > > been deleted using Taggings.count(). Can it be done using mocks > > instead? > > > > describe Book do > > > > before do > > @book = mock_model(Book) > > @tagging = mock_model(Tagging) > > @book.stub!(:taggings).and_return([@tagging, @tagging]) > > end > > > > it "should delete associated taggings when destroyed" do > > end > > > > end > > You have to create a real book, but you can create a mock tagging:Actually, you don''t have to save it - you can use #new instead of create: book = Book.new(:title => "the book") book.taggings << tagging = mock_model(Tagging, :[]= => true, :save => true) tagging.should_receive(:destroy) book.destroy> > Cheers, > David > > > > > > > thanks > > dave > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > >
David Green
2007-Jun-08 15:00 UTC
[rspec-users] checking associated objects have been deleted
On 6/7/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 6/7/07, David Green <pocketsized at tiscali.co.uk> wrote: > > I have the following model: > > > > class Book < ActiveRecord::Base > > has_many :taggings, :dependent => :delete_all > > has_many :tags, :through => :taggings > > end > > > > is it possible to check that associated taggings are being destroyed > > using mocks? I don''t want to test that rails is deleting the > > associations, I want to test that I have specified the association as > > a dependent one. > > > > the only way I can think of is to use fixtures (or create real > > associations in the before callback) and then check that they have > > been deleted using Taggings.count(). Can it be done using mocks > > instead? > > > > describe Book do > > > > before do > > @book = mock_model(Book) > > @tagging = mock_model(Tagging) > > @book.stub!(:taggings).and_return([@tagging, @tagging]) > > end > > > > it "should delete associated taggings when destroyed" do > > end > > > > end > > You have to create a real book, but you can create a mock tagging:Actually, you don''t have to save it - you can use #new instead of create: book = Book.new(:title => "the book") book.taggings << tagging = mock_model(Tagging, :[]= => true, :save => true) tagging.should_receive(:destroy) book.destroy> > Cheers, > David >hi David that spec fails because associations defined using :dependent => :delete_all don''t call the destroy() method on associated objects. I''m not sure if I can avoid creating and counting objects after all thanks dave -- View this message in context: http://www.nabble.com/checking-associated-objects-have-been-deleted-tf3882933.html#a11028074 Sent from the rspec-users mailing list archive at Nabble.com.
David Chelimsky
2007-Jun-08 15:02 UTC
[rspec-users] checking associated objects have been deleted
On 6/8/07, David Green <justnothing at tiscali.co.uk> wrote:> > > > On 6/7/07, David Chelimsky <dchelimsky at gmail.com> wrote: > > On 6/7/07, David Green <pocketsized at tiscali.co.uk> wrote: > > > I have the following model: > > > > > > class Book < ActiveRecord::Base > > > has_many :taggings, :dependent => :delete_all > > > has_many :tags, :through => :taggings > > > end > > > > > > is it possible to check that associated taggings are being destroyed > > > using mocks? I don''t want to test that rails is deleting the > > > associations, I want to test that I have specified the association as > > > a dependent one. > > > > > > the only way I can think of is to use fixtures (or create real > > > associations in the before callback) and then check that they have > > > been deleted using Taggings.count(). Can it be done using mocks > > > instead? > > > > > > describe Book do > > > > > > before do > > > @book = mock_model(Book) > > > @tagging = mock_model(Tagging) > > > @book.stub!(:taggings).and_return([@tagging, @tagging]) > > > end > > > > > > it "should delete associated taggings when destroyed" do > > > end > > > > > > end > > > > You have to create a real book, but you can create a mock tagging: > > Actually, you don''t have to save it - you can use #new instead of create: > > book = Book.new(:title => "the book") > book.taggings << tagging = mock_model(Tagging, :[]= => true, :save => true) > tagging.should_receive(:destroy) > book.destroy > > > > > Cheers, > > David > > > > hi David > > that spec fails because associations defined using :dependent => :delete_all > don''t call the destroy() method on associated objects. I''m not sure if I can > avoid creating and counting objects after allAh - i had set up :dependent => :destroy. Bummer.> > thanks > dave > -- > View this message in context: http://www.nabble.com/checking-associated-objects-have-been-deleted-tf3882933.html#a11028074 > Sent from the rspec-users mailing list archive at Nabble.com. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >