# Using RSpec without runner - inconsistent context evaluation

**URL:** <https://www.ruby-forum.com/t/using-rspec-without-runner-inconsistent-context-evaluation/60393>\
**Category:** Ruby\
**Created:** [May 13, 2006, 1:11am UTC](https://www.ruby-forum.com/t/using-rspec-without-runner-inconsistent-context-evaluation/60393 "2006-05-13T01:11:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suraj\_NSKurapati](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Suraj\_NSKurapati](https://www.ruby-forum.com/u/Suraj_NSKurapati)\
**Post date:** [May 13, 2006, 1:11am UTC](https://www.ruby-forum.com/t/using-rspec-without-runner-inconsistent-context-evaluation/60393/1 "2006-05-13T01:11:11Z")

</div>

-----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-----

---

<div class="post-metadata">

**Author:** ![Suraj\_NSKurapati](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Suraj\_NSKurapati](https://www.ruby-forum.com/u/Suraj_NSKurapati)\
**Post date:** [May 13, 2006, 2:48am UTC](https://www.ruby-forum.com/t/using-rspec-without-runner-inconsistent-context-evaluation/60393/2 "2006-05-13T02:48:10Z")

</div>

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:🏃‍♂️:ContextRunner.new(["-v"],[nil])  
Spec:🏃‍♂️: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-----

---

<div class="post-metadata">

**Author:** ![Suraj\_NSKurapati](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Suraj\_NSKurapati](https://www.ruby-forum.com/u/Suraj_NSKurapati)\
**Post date:** [May 13, 2006, 6:36am UTC](https://www.ruby-forum.com/t/using-rspec-without-runner-inconsistent-context-evaluation/60393/3 "2006-05-13T06:36:40Z")

</div>

-----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:🏃‍♂️:ContextRunner.new(ARGV)  
Spec:🏃‍♂️: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-----
