Style issue


#1

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?


#2

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


#3

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.


#4

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


#5

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)


#6

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)


#7

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.


#8

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