Forum: Ruby using RSpec without runner - inconsistent context evaluation

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.
Suraj N. Kurapati (Guest)
on 2006-05-13 03:11
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

I'm trying to use RSpec 0.5.3 with Ruby 1.8.4 in an embedded situation:

(C program
| (Ruby interpreter
| | (RSpec library
| |   ... my specification ...
| | )
| )
)

Here I have an embedded Ruby interpreter which can run any Ruby
file, but I do not have the ability to use the RSpec runner because
it only works with this kind of situation:

(RSpec runner
| (Ruby interpreter
| | ... my specification ...
| )
)


The only way I know to solve this problem is invoking the RSpec
library directly through through RubyGems' "require" method:

### begin stack_spec.rb ###
require 'rubygems'
require_gem 'rspec'
require 'spec'


context "A new stack" do
	specify "should be empty" do
	end
end

context "An empty stack" do
	specify "should not be empty after 'push'" do
	end
end
### end stack_spec.rb ###


However, the evaluation of the contexts is different when I use the
RSpec library directly (as with the stack_spec.rb above) and when I
use the RSpec runner:

### begin output ###
$ spec stack_spec.rb

..

Finished in 0.000854 seconds

2 contexts, 2 specifications, 0 failures


$ ruby stack_spec.rb

.

Finished in 0.000582 seconds

1 context, 1 specification, 0 failures

.

Finished in 0.000531 seconds

1 context, 1 specification, 0 failures
### end output ###


How can I use the RSpec library directly (through "require" or some
other means) and still get the same behavior as if I used the RSpec
runner?

Thanks for your attention.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEZRWFmV9O7RYnKMcRArc+AJ9tA3OxNiyukSseRRcUc1FzKJk3UwCfdm9u
wVR2dap5kdBXj+uVh0qkiBI=
=05Qn
-----END PGP SIGNATURE-----
Scott l H. (Guest)
on 2006-05-13 04:48
Your situation is very much different from mine but I think the solution
may be the same.

What is happening is that the contexts are being executed one by one in
the embedded (ruby) invocation versus "collected" and executed together
in the spec invocation.

The context class has a built in capability to attach itself to the
runner as they are being created, given all defaults. If you type the
contexts in to irb, you'd probably get similar behavior.

So what you'll need to do is replicate what the spec command is doing:

# create a runner and tell the Context class to use it from now on.
context_runner = Spec::Runner::ContextRunner.new(["-v"],[nil])
Spec::Runner::Context.context_runner = context_runner
# include your contexts and specs here
# then run them all at once.
context_runner.run

This is taken from the spec command that's in the bin folder of the
distribution 0.5.3

I'm hoping this helps.

Scott

Suraj N. Kurapati wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello,
>
> I'm trying to use RSpec 0.5.3 with Ruby 1.8.4 in an embedded situation:
>
> (C program
> | (Ruby interpreter
> | | (RSpec library
> | |   ... my specification ...
> | | )
> | )
> )
>
> Here I have an embedded Ruby interpreter which can run any Ruby
> file, but I do not have the ability to use the RSpec runner because
> it only works with this kind of situation:
>
> (RSpec runner
> | (Ruby interpreter
> | | ... my specification ...
> | )
> )
>
>
> The only way I know to solve this problem is invoking the RSpec
> library directly through through RubyGems' "require" method:
>
> ### begin stack_spec.rb ###
> require 'rubygems'
> require_gem 'rspec'
> require 'spec'
>
>
> context "A new stack" do
> 	specify "should be empty" do
> 	end
> end
>
> context "An empty stack" do
> 	specify "should not be empty after 'push'" do
> 	end
> end
> ### end stack_spec.rb ###
>
>
> However, the evaluation of the contexts is different when I use the
> RSpec library directly (as with the stack_spec.rb above) and when I
> use the RSpec runner:
>
> ### begin output ###
> $ spec stack_spec.rb
>
> ..
>
> Finished in 0.000854 seconds
>
> 2 contexts, 2 specifications, 0 failures
>
>
> $ ruby stack_spec.rb
>
> .
>
> Finished in 0.000582 seconds
>
> 1 context, 1 specification, 0 failures
>
> .
>
> Finished in 0.000531 seconds
>
> 1 context, 1 specification, 0 failures
> ### end output ###
>
>
> How can I use the RSpec library directly (through "require" or some
> other means) and still get the same behavior as if I used the RSpec
> runner?
>
> Thanks for your attention.
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
>
> iD8DBQFEZRWFmV9O7RYnKMcRArc+AJ9tA3OxNiyukSseRRcUc1FzKJk3UwCfdm9u
> wVR2dap5kdBXj+uVh0qkiBI=
> =05Qn
> -----END PGP SIGNATURE-----
Suraj N. Kurapati (Guest)
on 2006-05-13 08:36
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Scott L Holmes wrote:
>
> distribution 0.5.3
Thanks for your help Scott. The contexts are now evaluated as
expected. I'll post this as a feature request to the RSpec tracker.

### begin stack_spec.rb ###
require 'rubygems'
require_gem 'rspec'
require 'spec'

# bypass termination when no arguments are provided
ARGV.unshift ''

context_runner = Spec::Runner::ContextRunner.new(ARGV)
Spec::Runner::Context.context_runner = context_runner
at_exit {context_runner.run}


context "A new stack" do
	specify "should be empty" do
	end
end

context "An empty stack" do
	specify "should not be empty after 'push'" do
	end
end
### end stack_spec.rb ###

### begin output ###
$ ruby stack_spec.rb -v

A new stack
- - should be empty

An empty stack
- - should not be empty after 'push'


Finished in 0.001342 seconds

2 contexts, 2 specifications, 0 failures
### end output ###
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEZWIqmV9O7RYnKMcRArrQAJ9WXcmVsUNMLY51/AW77FwjVgeSxgCggKqa
lMP5DMEhnfBJLVU+fjh4CSo=
=hp7S
-----END PGP SIGNATURE-----
This topic is locked and can not be replied to.