I''m writing a Rails plugin that generates specs for Rails models. Obviously, I want to use RSpec to spec this plugin. The plugin, called ModelSpeccer, contains a module with three methods. Each method generates specs depending on its arguments. How can my plugin''s specs check that each method in ModelSpeccer created the correct specs? Thanks, Nick
>Each method generates specs depending on its argumentsHow does each method generate the specs? * Using script/generate rspec... or * File handling and writing your own? -- Joseph Wilk http://www.joesniff.co.uk Nick Hoffman wrote:> I''m writing a Rails plugin that generates specs for Rails models. > Obviously, I want to use RSpec to spec this plugin. > > The plugin, called ModelSpeccer, contains a module with three methods. > Each method generates specs depending on its arguments. How can my > plugin''s specs check that each method in ModelSpeccer created the > correct specs? > > Thanks, > Nick-- Posted via http://www.ruby-forum.com/.
On 2008-08-29, at 05:06, Joseph Wilk wrote:>> Each method generates specs depending on its arguments > > How does each method generate the specs? > * Using script/generate rspec... > or > * File handling and writing your own? > > -- > Joseph Wilk > http://www.joesniff.co.ukHi again Joseph. Each method generates the specs by calling RSpec''s #describe and #it methods, like this: def check_model_attributes_when_nil(model, ... ... columns_to_check.each do |attribute| describe model, "with ''#{attribute}'' set to nil" do ... it "should be invalid" do @model_instance.should_not be_valid end ... end end
I''ve been giving this some thought, I''ve not had the chance to test it out yet, but here are my examples: -- it "should add a ''it'' test" do example_group = Class.new(Spec::Example::ExampleGroup) example_group.should_receive(:it).with(...) ... example.class_eval do describe_model_attribute(model, attribute, valid_values, invalid_values) end end describe "valid model" it "should generate a spec which will pass" do example_group = Class.new(Spec::Example::ExampleGroup) do describe_model_attribute(model, attribute, valid_values, invalid_values) end example_group.run.should be_true end end -- So this means the scope of ''it'' and ''describe'' are against ExampleGroup. Giving you something to mock against to check ''it''/''describe''. Invoking run on the ExampleGroup class will run the specs you generate, and then you can check if they succeed/fail. An alternative to checking the output could be to test that the model has the relevant methods invoked on it. I hope that helps -- Joseph Wilk http://www.joesniff.co.uk Nick Hoffman wrote:> On 2008-08-29, at 05:06, Joseph Wilk wrote: >>> Each method generates specs depending on its arguments >> >> How does each method generate the specs? >> * Using script/generate rspec... >> or >> * File handling and writing your own? >> >> -- >> Joseph Wilk >> http://www.joesniff.co.uk > > Hi again Joseph. Each method generates the specs by calling RSpec''s > #describe and #it methods, like this: > > def check_model_attributes_when_nil(model, ... > ... > columns_to_check.each do |attribute| > describe model, "with ''#{attribute}'' set to nil" do > ... > it "should be invalid" do > @model_instance.should_not be_valid > end > ... > end > end-- Posted via http://www.ruby-forum.com/.
On 2008-08-31, at 15:40, Joseph Wilk wrote:> I''ve been giving this some thought, I''ve not had the chance to test it > out yet, > but here are my examples: > -- > it "should add a ''it'' test" do > example_group = Class.new(Spec::Example::ExampleGroup) > example_group.should_receive(:it).with(...) ... > > example.class_eval do > describe_model_attribute(model, attribute, valid_values, > invalid_values) > end > > end > > describe "valid model" > it "should generate a spec which will pass" do > example_group = Class.new(Spec::Example::ExampleGroup) do > describe_model_attribute(model, attribute, valid_values, > invalid_values) > end > example_group.run.should be_true > end > end > -- > > So this means the scope of ''it'' and ''describe'' are against > ExampleGroup. > Giving you something to mock against to check ''it''/''describe''. > > Invoking run on the ExampleGroup class will run the specs you > generate, > and then you can check if they succeed/fail. An alternative to > checking > the output could be to test that the model has the relevant methods > invoked on it. > > I hope that helps > -- > Joseph Wilk > http://www.joesniff.co.ukHi Joseph. I just realised that I hadn''t responded to this email of yours. Thanks for thinking about it and showing me how you''d take a stab at it. I''m going to give it another shot in the next few days. When I do, I''ll post my results. Cheers, Nick