# Strange Expectations answer

**URL:** <https://www.ruby-forum.com/t/strange-expectations-answer/135133>\
**Category:** Ruby\
**Created:** [April 18, 2008, 9:38am UTC](https://www.ruby-forum.com/t/strange-expectations-answer/135133 "2008-04-18T09:38:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 18, 2008, 9:38am UTC](https://www.ruby-forum.com/t/strange-expectations-answer/135133/1 "2008-04-18T09:38:14Z")

</div>

I am using Expectations and I found something which looks strange, when  
you call methods. For instance:

1. No problem with:

require ‘expectations’

count = []

Expectations do  
expect(0) { count.size }  
end

1. But when you add a second test, the _first one_ fails :

require ‘expectations’

count = []

Expectations do  
expect(0) { count.size }  
end

count \<\< “a”

Expectations do  
expect(1) { count.size }  
end

The first call to ‘count.size’ now returns 1.

I’d like, first to understand, and second to know to use Expectations  
when you follow a story like the one above. Thks.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 18, 2008, 10:04am UTC](https://www.ruby-forum.com/t/strange-expectations-answer/135133/2 "2008-04-18T10:04:43Z")

</div>

Michel D. wrote:

> expect(0) { count.size }  
> end  
> when you follow a story like the one above. Thks.

To put it bluntly - you shouldn’t do it that way.  
Expectations collects all expectations in a file and then runs them.  
Basing your tests on outside variables is not a good idea. Restructure  
your tests to look like this:

Expectations do  
expect(0) do  
count = []  
count.size  
end

expect(1) do  
count = []  
count \<\< “a”  
count.size  
end  
end

Cheers

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JRuby Core Developer  
Developer, ThoughtWorks Studios ([http://studios.thoughtworks.com](http://studios.thoughtworks.com))  
Practical JRuby on Rails ([http://apress.com/book/view/9781590598818](http://apress.com/book/view/9781590598818))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 18, 2008, 10:20am UTC](https://www.ruby-forum.com/t/strange-expectations-answer/135133/3 "2008-04-18T10:20:37Z")

</div>

> Expectations collects all expectations in a file and then runs them.  
> Basing your tests on outside variables is not a good idea. Restructure  
> your tests to look like this:
> 
> Expectations do  
> expect(0) do  
> count = []  
> count.size  
> end
> 
> expect(1) do  
> count = []  
> count \<\< “a”  
> count.size  
> end  
> end

Thanks Ola.

Which means that one should not use Expectations when following a story.  
At step n, one would have to write again all preceeding steps. And have  
them rerun, with quadratic growth… Back to spec.
