How would you spec out something like the following: def a_method x = Class.new do include Enumerable end # do something here with x end describe "The Anonymous Class" do before :each do @anonymous_class = mock Class Class.stub!(:new).and_return @anonymous_class end it "should create a new anonymous class" do Class.should_receive(:new) a_method end it "should include Enumerable" do #... what goes here? end end One thought I''ve had on this is that we should be able to do something like this: it "should include Enumerable" do Class.should_receive(:new).with(&lambda { include Enumerable }).and_return true end The serious problem with implementing this is that in Ruby 1.8.6 proc equality occurs at the syntax/parser tree level, so this spec would fail: describe "Two procs" do it "should be equal with the same bodies" do @proc1 = lambda { do_something } @proc2 = lambda { do_something } @proc1.should == @proc2 end end Any good thoughts on how to get around this? Scott
On 14/8/07 21:30, "Scott Taylor" <mailing_lists at railsnewbie.com> wrote: > How would you spec out something like the following: > > def a_method > x = Class.new do > include Enumerable > end > > # do something here with x > end > > > describe "The Anonymous Class" do > > before :each do > @anonymous_class = mock Class > Class.stub!(:new).and_return @anonymous_class > end > > it "should create a new anonymous class" do > Class.should_receive(:new) > a_method > end > > it "should include Enumerable" do > #... what goes here? > end > > end Not being an expert, so apply the relevant pinches of salt! ...but if you''re including enumerable, then it must be so that you can enumerate the class, so would the behavior driven approach be that you would test the enumerable behavior you wish the class to exhibit rather than the implementation of enabling this behavior by including the enumerable module. Cheers Rupert -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070814/0379a970/attachment-0001.html
On 8/14/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote:> > How would you spec out something like the following: >You''re starting with the code, trying to tape on the spec afterwards. This is always a difficult task and is exactly what BDD and RSpec is trying to get you away from. Have you tried writing the RSpec examples first? I''d help you with an example if I knew what you''re trying to achieve from a functional (not code) standpoint. Aslak> def a_method > x = Class.new do > include Enumerable > end > > # do something here with x > end > > > describe "The Anonymous Class" do > > before :each do > @anonymous_class = mock Class > Class.stub!(:new).and_return @anonymous_class > end > > it "should create a new anonymous class" do > Class.should_receive(:new) > a_method > end > > it "should include Enumerable" do > #... what goes here? > end > > end > > One thought I''ve had on this is that we should be able to do > something like this: > > it "should include Enumerable" do > Class.should_receive(:new).with(&lambda { > include Enumerable > }).and_return true > end > > The serious problem with implementing this is that in Ruby 1.8.6 proc > equality occurs at the syntax/parser tree level, so this spec would > fail: > > describe "Two procs" do > it "should be equal with the same bodies" do > @proc1 = lambda { do_something } > @proc2 = lambda { do_something } > > @proc1.should == @proc2 > end > end > > Any good thoughts on how to get around this? > > Scott > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On Aug 14, 2007, at 5:17 PM, aslak hellesoy wrote:> On 8/14/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote: >> >> How would you spec out something like the following: >> > > You''re starting with the code, trying to tape on the spec afterwards. > This is always a difficult task and is exactly what BDD and RSpec is > trying to get you away from. > > Have you tried writing the RSpec examples first? I''d help you with an > example if I knew what you''re trying to achieve from a functional (not > code) standpoint.Sounds fair. The only reason I included the code first and presented it in that way was for simplicity. I''ll now go for the complexity: I already have developed test first, and running specs. I''d now like to refactor, but my specs are preventing me. The code is here: http://pastie.caboo.se/87722 Here is the code I''d like to refactor: def anonymous_class_with_loaded_modules(*mods) klass = Class.new mods.each do |mod| klass.send(:include, mod) end return klass end I''d like to change it to this, which is functionally equivalent: def anonymous_class_with_loaded_modules(*mods) klass = Class.new do mods.each do |mod| include, mod end end return klass end Sorry for the long code samples, but I''m not exactly sure how to simplify them. I can also give you a high level over-view of what this code is doing and what it''s purpose is. Let me know if that would better help you help me. Thanks for listening, Scott
On Aug 14, 2007, at 5:53 PM, Scott Taylor wrote:> > On Aug 14, 2007, at 5:17 PM, aslak hellesoy wrote: > >> On 8/14/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote: >>> >>> How would you spec out something like the following: >>> >> >> You''re starting with the code, trying to tape on the spec afterwards. >> This is always a difficult task and is exactly what BDD and RSpec is >> trying to get you away from. >> >> Have you tried writing the RSpec examples first? I''d help you with an >> example if I knew what you''re trying to achieve from a functional >> (not >> code) standpoint. > > > Sounds fair. The only reason I included the code first and presented > it in that way was for simplicity. I''ll now go for the complexity: > > I already have developed test first, and running specs. I''d now like > to refactor, but my specs are preventing me. The code is here: > > http://pastie.caboo.se/87722 > > Here is the code I''d like to refactor: > > def anonymous_class_with_loaded_modules(*mods) > klass = Class.new > mods.each do |mod| > klass.send(:include, mod) > end > return klass > end > > I''d like to change it to this, which is functionally equivalent: > > def anonymous_class_with_loaded_modules(*mods) > klass = Class.new do > mods.each do |mod| > include, mod > end > end > return klass > endAslak, Maybe dependency injection would be the way to solve this problem. Let me know your thoughts - I''d like to clean up this code. Scott
Apparently Analagous Threads
- [ win32utils-Bugs-28840 ] wrong process_id is returned if using create multiple times for IE
- elegant way to check if 2 values are in 3 columns?
- pivot table
- dtracing a forked process OR dynamic library
- Matching Problem: Want to match to data.frame with inexact matching identifier (one identifier has to be in the range of the other).