2

As for now I have something like this in my rspec test and it works fine:

expect{
 ...
}.to change(@foo_array.first, :bar).from(3.14).to(69)

But how to expect the change not only for .first but for every at @foo_array ?

Note: important is to don't execute ... commands multiple times

pawel7318
  • 3,383
  • 2
  • 28
  • 44
  • Try to use `change{@foo_array.first.bar}` (with braces) instead of `change(@foo_array.first, :bar)`. – ole May 11 '14 at 19:49
  • It's not the point, the test I have works fine but only for the `.first` array element and I wish to test all elements. I'm reading about `collect`, `inject` and `map` wondering how to use one of them but I'm not even sure if it's a good approach. – pawel7318 May 11 '14 at 20:05

1 Answers1

2

How about:

count = @foo_array.count
expect{
 ...
}.to change(@foo_array.map(&:bar)).from([3.14]*count).to([69]*count)
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • I got `nil is not a symbol` error but it's not clear from what.. I don't understand `.map` to `&` much but I feel this can be a problem here. I can provide more details for exact case I have. – pawel7318 May 11 '14 at 20:22
  • Probably I got something that works using your advice: `expect{ ... }.to change{@foo_array.map {|foo| foo.bar }}.from([3.14]*count).to([69]*count)`. I need to play a bit with it to make sure – pawel7318 May 11 '14 at 20:26
  • It works (my version). I accepted the answer as this pushed me to the final solution and I want to hold on a bit with editing it as maybe the way you build it with `&:bar` should work but I made a mistake. Please take a look and comment as I'm not sure how `&:bar` should work. – pawel7318 May 11 '14 at 20:43
  • `x.map(&:bar)` is equivalent to `x.map { |f| f.bar }` see here: http://stackoverflow.com/questions/1961030/ruby-ampersand-colon-shortcut I believe you might have missed the `:` (wrote `.map(&bar)` by mistake) – Uri Agassi May 12 '14 at 03:55