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.
James B. (Guest)
on 2009-04-23 23: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?
Mark W. (Guest)
on 2009-04-24 00:00
(Received via mailing list)
On Thu, Apr 23, 2009 at 12:05 PM, James B. 
<removed_email_address@domain.invalid>
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
James B. (Guest)
on 2009-04-24 00:18
Mark W. 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.
Mark W. (Guest)
on 2009-04-24 02:39
(Received via mailing list)
On Thu, Apr 23, 2009 at 1:18 PM, James B. <removed_email_address@domain.invalid>
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
Zach D. (Guest)
on 2009-04-24 04:13
(Received via mailing list)
On Thu, Apr 23, 2009 at 3:59 PM, Mark W. <removed_email_address@domain.invalid> 
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
> removed_email_address@domain.invalid
> http://rubyforge.org/mailman/listinfo/rspec-users
>



--
Zach D.
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
@zachdennis (twitter)
Zach D. (Guest)
on 2009-04-24 04:23
(Received via mailing list)
On Thu, Apr 23, 2009 at 8:11 PM, Zach D. <removed_email_address@domain.invalid>
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 D.
> http://www.continuousthinking.com (personal)
> http://www.mutuallyhuman.com (hire me)
> @zachdennis (twitter)
>



--
Zach D.
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
@zachdennis (twitter)
Matt W. (Guest)
on 2009-04-24 11:51
(Received via mailing list)
On 23 Apr 2009, at 20:05, James B. 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 W.
http://beta.songkick.com
http://blog.mattwynne.net
James B. (Guest)
on 2009-04-24 17:26
Matt W. 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.