[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

strange Expectations answer

Michel Demazure

4/18/2008 7:38:00 AM

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

2. 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.
--
Posted via http://www.ruby-....

2 Answers

Ola Bini

4/18/2008 8:04:00 AM

0

Michel Demazure wrote:
> 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
>
> 2. 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.
>
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 Bini (http://ola-bini.bl...)
JRuby Core Developer
Developer, ThoughtWorks Studios (http://studios.though...)
Practical JRuby on Rails (http://apress.com/book/view/978...)

"Yields falsehood when quined" yields falsehood when quined.



Michel Demazure

4/18/2008 8:21:00 AM

0


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


--
Posted via http://www.ruby-....