Hi I am new to rspec and was trying to figure out what is the difference between passing a block to expect{ } and just using expect( )
Here is a trivial example
require "rails_helper"
RSpec.describe "Test",type: :model do
it "testing count" do
arr=[1,2,3]
expect{arr<<1}.to change{arr.count}.by(1)
end
end
This runs fine but when I do this instead
require "rails_helper"
RSpec.describe "Test",type: :model do
it "testing count" do
arr=[1,2,3]
expect(arr<<1).to change{arr.count}.by(1)
end
end
It throws an error
Failures:
1) Test testing count
Failure/Error: expect(arr<<1).to change{arr.count}.by(1)
expected `arr.count` to have changed by 1, but was not given a block
# ./spec/models/test_spec.rb:6:in block (2 levels) in <top (required)>
And even when I don't use a block with change it gives me an error
require "rails_helper"
RSpec.describe "Test",type: :model do
it "testing count" do
arr=[1,2,3]
expect{arr<<1}.to change(arr.count).by(1)
end
end
Failures:
1) Test testing count
Failure/Error: expect{arr<<1}.to change(arr.count).by(1)
ArgumentError:
`change` requires either an object and message (`change(obj, :msg)`) or a block (`change { }`). You passed an object but no message.
Can someone explain why is it happening??