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?
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?
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
invalid_values)
example_group.run.should be_true
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.
Hi 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.
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.