ruby on rails - RSpec 'change': Braces or brackets? -


i following michael hartl's rails tutorial , there rspec testing code snippet:

expect  click_button 'follow' end.to change(user.followed_users, :count).by(1)` 

according rspec documentation , how learned on codeschool, should be:

expect  click_button 'follow' end.to change {user.followed_users.count}.by(1) 

obviously both valid, there seems no documentation first way of doing change matcher in rspec documentation , wondering why/how works.

i couldn't find in relish documentation either, rspec open source after all, let's dig in. in the current version of change matcher:

module rspec   module matchers     module builtin       class change         def initialize(receiver=nil, message=nil, &block)           @message = message           @value_proc = block || lambda {receiver.__send__(message)}           @expected_after = @expected_before = @minimum = @maximum = @expected_delta = nil           @eval_before = @eval_after = false         end 

it's @value_proc. if supply block, uses that; , if don't supply block, makes new lambda sends second argument first argument. if read through source little more, you'll see #matches? method, contains:

@actual_before = evaluate_value_proc event_proc.call @actual_after = evaluate_value_proc 

where evaluate_value_proc @value_proc.call.

i think "conventional" rspec have supply explicit block. two-argument approach might make more sense if you're metaprogramming, then, could use block approach , call send yourself.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -