Skip to content

Instantly share code, notes, and snippets.

@anlek
Last active December 24, 2015 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anlek/6833991 to your computer and use it in GitHub Desktop.
Save anlek/6833991 to your computer and use it in GitHub Desktop.
Mocha expects without casting
#Only include this during your tests, no need in Development or production.
class Hash
def stringify
inject({}){|memo,(k,v)| memo[k.to_s] = v.to_s; memo}
end
end
FactoryGirl.define do
factory :price do
amount {rand(1..99).to_f}
name "MyString"
brief "MyString"
target {Price::VALID_TARGETS.sample.to_sym}
association :product, strategy: :build
end
end
product.prices.any_instance.expects(:update).with(valid_attributes.stringify_keys)
put :update, {id: price.to_param, product_id: product.id, price: valid_attributes}, valid_session
PricesController PUT update with valid params updates the requested price
Failure/Error: put :update, {id: price.to_param, product_id: product.id, price: valid_attributes}, valid_session
MiniTest::Assertion:
unexpected invocation: #<AnyInstance:Price>.update('amount' => '46.0', 'name' => 'MyString', 'brief' => 'MyString', 'target' => 'distributor')
unsatisfied expectations:
- expected exactly once, not yet invoked: #<AnyInstance:Price>.update('amount' => 46.0, 'name' => 'MyString', 'brief' => 'MyString', 'target' => :distributor)
@anlek
Copy link
Author

anlek commented Oct 4, 2013

Fixed issue by stringifying everything in the valid_attributes:

product.prices.any_instance.expects(:update).with(valid_attributes.stringify)
put :update, {id: price.to_param, product_id: product.id, price: valid_attributes}, valid_session

@anlek
Copy link
Author

anlek commented Oct 4, 2013

Also see the hash_ext.rb file. I added it to my spec/support folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment