Forum: RSpec Style issue

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.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-04-23 21:05
In one of my features I have to check to see if a file is created by an
external script and then determine if that file contains specific
information.  So, I ended up with these step definitions:


When /currency exchange rate transfer file should contain rates/ do
  found = false
  fx_code = 'USD'
  File.open(FOREX_XFR_FN).each do |line|
    found = true if
/.*fx_target#{fx_code}.*fx_rate(\d+\.\d{4}).*/.match(line)
    break if found
  end
  fail(ArgumentError, "Exchange Rate not found for #{fx_code}") if not
found
end

When /currency exchange rate transfer file should exist/ do
  File.open(FOREX_XFR_FN)
end


These work well enough.  What I want to know if this is considered a
normal style for testing this sort of specification and if not then what
is?
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2009-04-23 22:00
(Received via mailing list)
On Thu, Apr 23, 2009 at 12:05 PM, James Byrne <lists@ruby-forum.com>
wrote:

> end
I find this more straighforward and easy to understand:

 When /currency exchange rate transfer file should contain rates/ do
  fx_code = 'USD'
  File.open(FOREX_XFR_FN).each do |line|
    return true if
/.*fx_target#{fx_code}.*fx_rate(\d+\.\d{4}).*/.match(line)
  end
  fail(ArgumentError, "Exchange Rate not found for #{fx_code}")
 end

///ark
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-04-23 22:18
Mark Wilden wrote:

> I find this more straighforward and easy to understand:
>
>  When /currency exchange rate transfer file should contain rates/ do
>   fx_code = 'USD'
>   File.open(FOREX_XFR_FN).each do |line|
>     return true if
> /.*fx_target#{fx_code}.*fx_rate(\d+\.\d{4}).*/.match(line)
>   end
>   fail(ArgumentError, "Exchange Rate not found for #{fx_code}")
>  end
>
> ///ark

If I do a return from inside the iterator then I get a jump error.  It
has to be break, which is how I ended up with what I did.
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2009-04-24 00:39
(Received via mailing list)
On Thu, Apr 23, 2009 at 1:18 PM, James Byrne <lists@ruby-forum.com>
wrote:

> >   fail(ArgumentError, "Exchange Rate not found for #{fx_code}")
> >  end
> >
> > ///ark
>
> If I do a return from inside the iterator then I get a jump error.  It
> has to be break, which is how I ended up with what I did.


Oh, right. I believe this behavior is changing in Ruby 1.9.

///ark
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2009-04-24 02:13
(Received via mailing list)
On Thu, Apr 23, 2009 at 3:59 PM, Mark Wilden <mark@mwilden.com> wrote:
>> fail(ArgumentError, "Exchange Rate not found for #{fx_code}") if not
>   end
>   fail(ArgumentError, "Exchange Rate not found for #{fx_code}")
>  end

I'd even vote for the more concise form:

When /currency exchange rate transfer file should contain rates/ do
  fx_code = 'USD'
  IO.readlines(FOREX_XFR_FN).any?{ |line|
    line.match /.*fx_target#{fx_code}.*fx_rate(\d+\.\d{4}).*/
  } || fail ArgumentError, "Exchange Rate not found for #{fx_code}"
unless found
end


>
> ///ark
>
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



--
Zach Dennis
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
@zachdennis (twitter)
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2009-04-24 02:23
(Received via mailing list)
On Thu, Apr 23, 2009 at 8:11 PM, Zach Dennis <zach.dennis@gmail.com>
wrote:
>>> end
>> /.*fx_target#{fx_code}.*fx_rate(\d+\.\d{4}).*/.match(line)
>  } || fail ArgumentError, "Exchange Rate not found for #{fx_code}" unless found
> end

Whoops, make that...

When /currency exchange rate transfer file should contain rates/ do
 fx_code = 'USD'
 IO.readlines(FOREX_XFR_FN).any?{ |line|
   line.match /.*fx_target#{fx_code}.*fx_rate(\d+\.\d{4}).*/
 } || fail(ArgumentError, "Exchange Rate not found for #{fx_code}")
end

>
>
>
> --
> Zach Dennis
> http://www.continuousthinking.com (personal)
> http://www.mutuallyhuman.com (hire me)
> @zachdennis (twitter)
>



--
Zach Dennis
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
@zachdennis (twitter)
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2009-04-24 09:51
(Received via mailing list)
On 23 Apr 2009, at 20:05, James Byrne wrote:

>    found = true if
>
>
> These work well enough.  What I want to know if this is considered a
> normal style for testing this sort of specification and if not then
> what
> is?

Slightly off-topic, but it seems a bit weird to have the word 'should'
in a When step to me. Are these really Then steps?

Matt Wynne
http://beta.songkick.com
http://blog.mattwynne.net
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-04-24 15:26
Matt Wynne wrote:

> Slightly off-topic, but it seems a bit weird to have the word 'should'
> in a When step to me. Are these really Then steps?
>

Actually, all of my step definitions are always Thens.  This one is a
cut & paste artifact that I overlooked.
This topic is locked and can not be replied to.