similar to: stub calls to an instance method before it is created?

Displaying 20 results from an estimated 2000 matches similar to: "stub calls to an instance method before it is created?"

2007 Mar 27
3
Stubbing out method for all instances
I am using the acts_as_state_machine plugin to control state of an object in my app, however when testing this I need to be able to stub out the guard conditions so that state will change when I fire off an event without depending on other models. Guard conditions simply return true or false so I have an instance method: def encoded? <check state of other objects> return true
2008 Jun 04
6
any_instance
Hi folks. I''m very interested in the status of a port of any_instance from Mocha to RSpec. In particular I have a spec suite that I wrote using RSpec but with Mocha that depends heavily on any_instance. This is because it stubs out one or two methods from an underlying API. In attempting to convert the suite to RSpec mocks (because that''s what we''re using for the rest
2006 Oct 13
6
If I stub do I have to mock as well?
Hi, I''m new to mocha and stubba but eager to learn. I have a rails functional test in which I would like to stub an instance method of particular class to always return true. I tried the following: def test_post_checkout Order.any_instance.stubs(:successful?).returns(true) post :checkout assert_response :redirect assert_equal "Checkout was successful.",
2006 Dec 21
3
Stubbing an instance method on a class
Hi all, I''m not sure if I''ve missed it, but I couldn''t find any docs for stubbing an instance method on a class. For example: class Foo; end Foo.stub!(:my_instance_method).and_return(nil) The reason (I think) I need this is because a certain class (Test::Unit::TestCase) expects an argument (name of method) to new and throws an exception if that instance method
2006 Dec 02
3
Scoping expectations and more ...
Hello! I''m new to Mocha and through experimentation I found out something that is not explicitly stated in the documentation: If you need to mock the same function call with different parameters and results, you can set the scope of expectations with a begin ... end block. I also made up an example on how to expect more than one function call with different parameters and return values.
2007 Feb 06
3
Using any_instance with Rails to make sure the right thing is saved?
I''d like to test that an object with the correct attributes is saved, something like: def test_create_without_existing_suite this = self w = Annotation.any_instance.expects(:save).with do |x| this.assert_equal ''fnord'', some_way_get_the_name_of_the_receiver_of_save true end w.returns true end But the object is created inside the method
2006 Dec 21
4
Stubbing Kernel#open
Anyone know how to stub Kernel#open? I''m trying to mock/stub an open-uri call, but it doesn''t seem to like it. Here''s the test code, and the failures: body = File.open(File.dirname(__FILE__) + ''/../fixtures/google_search_california.html'').read
2007 Mar 30
7
problem with using any_instance
Hey all, I have a question with using mocha in my tests. In the same test file, I have two tests, <code> def test_a klass.any_instance.stubs(:method_name).returns("something") klass.new.method_name ... end def test_b ... klass.new.method_name ... end </code> where klass is some class when the tests run, test _a passes, but test_b had an error like this:
2007 Jun 11
12
Mocking system/`
This drives me insane on a regular basis. How does one mock system(''blah'') or `blah` ? Adding expectations on Kernel doesn''t do it. Adding expectations on Object just makes me sad: Object.any_instance.expects(:system).with(''ls'') # => #<Mock:0x12b584e>.system(''ls'') - expected calls: 0, actual calls: 1 And this really
2011 May 28
4
how can I investigate when a rspec test fails?
For example: it "re-renders the ''new'' template" do # Trigger the behavior that occurs when invalid params are submitted Sector.any_instance.stub(:save).and_return(false) post :create, :sector => {} response.should render_template("new") end I have the new template under app/views/sectors but the test says: SectorsController POST
2006 Jan 25
7
Ruby on Rails Presentation Wed (1/24) night in Menlo Park, CA
I''ll be giving a 60 to 90 minute presentation covering Ruby on Rails in the San Francisco bay area. Anyone interested in a quick introduction to Rails is welcome to come. The talk will be focused on Rails, but we''ll also field questions about how the rest of our site (Zvents) works. Here are the event details: http://www.zvents.com/events/show/211380 If you''re
2011 Aug 04
3
#create tests fail when I add FriendlyId to my model
I am doing controller testing, and I can''t seem to get the create method test to pass when friendly_id is added to the mix. If I comment it out of the model, the tests all pass perfectly. The moment I add it back in, the error looks like this: 1) Error: test_create_valid(BrandsControllerTest): FriendlyId::BlankError: FriendlyId::BlankError
2007 Jun 12
4
advice on new failures with 0.5.0
Hi all, We just updated to the latest version of Mocha and now we''re seeing a lot of odd test failures. If this isn''t the appropriate place for a question like this, please let me know. Say I have code like this in a controller: DataMonitor.toggle_notification! And I mock it like this: DataMonitor.any_instance.stubs(:toggle_notification!) This would work fine with Mocha
2006 Oct 17
4
Mocking the rendering of a Rails template
Hello all, I''m having difficulty setting up mocks such that I can verify that Rails was going to render the right template file (for example new.rhtml) and stop Rails from performing the actual rendering. After peeking at Rails'' internals, I tried two techniques as illustrated here: http://pastie.caboo.se/18197 Neither worked. I think the first one failed because Rails
2013 Jan 29
0
How to stub a class inside a module with Mocha in Ruby?
I know there are lot of questions in here around this. I''ve tried all of them but none of them works. Basically, I''m trying to stub TwitterOAuth::Client so my test won''t call the real api every time. Here''s the bit from TwitterOAuth gem that I use. module TwitterOAuth class Client def initialize(options = {}) @consumer_key =
2008 Jan 30
2
Testing a call to super
Did any further discussion ever come of this? I take it is officially not supported currently? I just get an odd error when I try it (it throws a "0 expected 1 received", no matter what stubbing/expectation combo I use), so came looking around on google, and finally the mailinglist. Here''s my stripped down example: http://pastie.caboo.se/145603 (it fails with expects too)
2007 Mar 04
4
Rails functional testing and Mocha
I''ve always wanted to be able to do stuff like this in my functional tests c = customers(:customer_1) c.expects(:great_customer_service) post :service_customer, :id => c.id This of course fails because inside the rails action a different instance of customer is used. Some of the time setting your expectation/stubbing on Customer.any_instance works, but it''s not
2005 Nov 08
2
IE not repopulating forms under Rails views - very odd
It is common practice to implement semi-persistent state across page views using hidden form fields since all major browsers repopulate the form fields with the last value of each field (provided that the browser hasn''t been closed). A longer explanation with samples is here: http://codinginparadise.org/weblog/2005/08/ajax-tutorial-saving-session-across.html There''s a very
2007 Jul 30
4
Stubbing Observers in rails?
In most of my tests I''d like to be able to stub out the observers for my models, but I''m not sure the best way to do this. I doesn''t look like there is a way to stub all instance methods, and I don''t seem to be able to stub early enough to stub out the observer as it''s instantiated. I can think of several hackish ways to get around it, but I was
2007 Jul 30
6
Object.stubs doesn''t seem to work.
Hi I''m using Mocha 0.5.3 and I want to stub out a call to Time.now, just like the example in the post http://blog.floehopper.org/articles/2007/06/08/mocha-0-5-released However, trying it in irb gets me the following error: >> require ''mocha'' => true >> Time.stubs(:now).returns(Time.parse(''Thu Feb 01 00:00:00 UTC 2007'')) NoMethodError: