Forum: RSpec Problems when programmatically defining examples

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jake B. (Guest)
on 2008-10-18 20:13
(Received via mailing list)
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?

Thanks in advance,
Jake
David C. (Guest)
on 2008-10-18 20:50
(Received via mailing list)
On Sat, Oct 18, 2008 at 2:13 PM, Jake B. <removed_email_address@domain.invalid> 
wrote:
>
>                     examples =
> 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/Spe...
for more info.

Cheers,
David
Jake B. (Guest)
on 2008-10-19 02:35
(Received via mailing list)
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

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
Zach D. (Guest)
on 2008-10-19 05:24
(Received via mailing list)
On Sat, Oct 18, 2008 at 6:34 PM, Jake B. <removed_email_address@domain.invalid> 
wrote:
>     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



> ~~~
>> On Sat, Oct 18, 2008 at 2:13 PM, Jake B. <removed_email_address@domain.invalid> wrote:
>> > test_expectation.example_groups_for(system_state)
>> >
>> > for
>> If I understand your question correctly, you can do this:
>> David
>> _______________________________________________
>> rspec-users mailing list
>> removed_email_address@domain.invalid
>> http://rubyforge.org/mailman/listinfo/rspec-users
>
>
> _______________________________________________
> rspec-users mailing list
> removed_email_address@domain.invalid
> http://rubyforge.org/mailman/listinfo/rspec-users
>



--
Zach D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com
Jake B. (Guest)
on 2008-10-19 21:41
(Received via mailing list)
Hi Zach,

That worked a treat. Thanks!

Regards,
Jake
This topic is locked and can not be replied to.