On 11 Apr 2008, at 14:09, V. Vincesf wrote:
>
> class UserTest < Test::Unit::TestCase
> def test_with_error
> u = User.new
> User.any_instance.stubs(:my_special_method).returns(lambda
> {u.errors.add(:name, "wrong name")})
> foo = User.new
> foo.my_my_special_method
> pp foo.errors # <= works
> foo = User.new default_user
> foo.my_special_method
> pp foo.valid?
> pp foo.errors # <= still works
> end
>
> Only issue with this code is that it throws a warning from Mocha
> telling
> me that lambda is deprecated. I can''t find a way to make it works
in
> any
> other way though.
in your lambda, you are referencing u, so when you call
my_special_method on foo, it is still adding the errors to u.
Fundamentally though stubs aren''t really suppose to do anything,
whereas you are trying to make them do something.
Fred>
> def test_with_error_case2
> u = User.new
>
> User
> .any_instance.stubs(:my_special_method).returns(u.errors.add(:name,
> "wrong name"))
> foo = User.new
> foo.my_special_method
> pp foo.errors # <= can see the error
> foo.valid?
> foo.my_special_method
> pp foo #<= no error has been set
> end
> Adding times(n) to the stubs doesn''t help me.
>
> Thank you for your help as I''m clearly missing something here
>
> Vince
> --
> Posted via http://www.ruby-forum.com/.
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---