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? -- Posted via http://www.ruby-forum.com/.
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 > > ///arkIf 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. -- Posted via http://www.ruby-forum.com/.
On Thu, Apr 23, 2009 at 3:59 PM, Mark Wilden <mark at mwilden.com> wrote:> ?On Thu, Apr 23, 2009 at 12:05 PM, James Byrne <lists at ruby-forum.com> wrote: > >> 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 > > 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}") > ?endI''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 at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Zach Dennis http://www.continuousthinking.com (personal) http://www.mutuallyhuman.com (hire me) @zachdennis (twitter)
On Thu, Apr 23, 2009 at 8:11 PM, Zach Dennis <zach.dennis at gmail.com> wrote:> On Thu, Apr 23, 2009 at 3:59 PM, Mark Wilden <mark at mwilden.com> wrote: >> ?On Thu, Apr 23, 2009 at 12:05 PM, James Byrne <lists at ruby-forum.com> wrote: >> >>> 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 >> >> 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 > > 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 > endWhoops, 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> > >> >> ///ark >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > > -- > 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)
On 23 Apr 2009, at 20:05, James Byrne wrote:> 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?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
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. -- Posted via http://www.ruby-forum.com/.