[rspec-users] Problems when programmatically defining examples

Zach Dennis zach.dennis at gmail.com
Sat Oct 18 21:24:01 EDT 2008


On Sat, Oct 18, 2008 at 6:34 PM, Jake Benilov <benilov at gmail.com> wrote:
> David,
>
> Thanks for your reply; however I probably wasn't very clear in my
> explanation. What I am really trying to do is to create a builder for
> example group objects, without automatically adding the example groups to
> the rspec runner when the builder code is invoked.
>
> As an example, when I call the following code:
>
> describe "a group" do
>     examples = Class.new(Spec::Example::ExampleGroup).describe("example")
>     examples.it "should not be added to the outer group" do
>         true.should be_false
>     end
> end

When you create a subclass of Spec:Example::ExampleGroup, it
automatically gets registered. You want to make sure you unregister it
so rspec's Runner doesn't try to run it. Try this:

describe "a group" do
    example_group = Class.new(Spec::Example::ExampleGroup)
    example_group.unregister
    examples = example_group.describe("example")
    examples.it "should not be added to the outer group" do
        true.should be_false
    end
end



>
> I get:
> ~~~
> 1)
> 'example should not be added to the outer group' FAILED
> expected false, got true
>
> Finished in 0.027012 seconds
>
> 1 example, 1 failure
> ~~~
> This is probably expected, but what I really want is that the "examples"
> example group is NOT picked up by rspec. I suppose that I need to stay away
> from the "it" and "describe" methods... right?
>
> Regards,
> Jake
>
> On Sat, Oct 18, 2008 at 6:50 PM, David Chelimsky <dchelimsky at gmail.com>
> wrote:
>>
>> On Sat, Oct 18, 2008 at 2:13 PM, Jake Benilov <benilov at gmail.com> wrote:
>> > Hi,
>> >
>> > I am trying to code an application that is based on Rspec; I am
>> > programmatically building examples, and launching the runner with a
>> > custom
>> > formatter. Here are the code snippets from my app:
>> >
>> > Launching the runner:
>> >
>> >                 example_groups =
>> > test_expectation.example_groups_for(system_state)
>> >
>> >                 @output = StringIO.new
>> >                 options = Spec::Runner::OptionParser.parse(["--format",
>> > "RAutotest::Runner::Formatter"], @output, @output)
>> >                 example_groups.each {|example_group|
>> > options.add_example_group(example_group) }
>> >
>> >                 Spec::Runner::CommandLine.run(options)
>> >
>> > Building the example groups:
>> >
>> >                     examples =
>> > Class.new(Spec::Example::ExampleGroup).describe("Statistics")
>> >                     @expectations.map do |expectation|
>> >                         examples.it examples.description do
>> >                             actual_stats_counters.should expectation
>> >                         end
>> >                     end
>> >                     examples
>> >
>> > This is working fine, except for one problem. When I am writing examples
>> > for
>> > my app (also using rspec), the examples that are generated within my
>> > application (the inner examples, so to say) are being added to the
>> > application's examples (the outer examples).
>> > This means that if inner expected failures are causing my outer examples
>> > to
>> > fail.
>> >
>> > How is it possible for me to verify expected failures without causing my
>> > examples to fail?
>>
>> If I understand your question correctly, you can do this:
>>
>> lambda {
>>  # stuff that should fail
>> }.should raise_error(Spec::Expectations::ExpectationNotMetError)
>>
>> See
>> http://rspec.rubyforge.org/rspec/1.1.8/classes/Spec/Matchers.html#M000434
>> for more info.
>>
>> Cheers,
>> David
>>
>> >
>> > Thanks in advance,
>> > Jake
>> >
>> > _______________________________________________
>> > rspec-users mailing list
>> > rspec-users at rubyforge.org
>> > http://rubyforge.org/mailman/listinfo/rspec-users
>> >
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



-- 
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com


More information about the rspec-users mailing list