search for: any_instance

Displaying 20 results from an estimated 30 matches for "any_instance".

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''r...
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 shouldn''t work (and doesn''t): Object.expects(:system).with(''ls'') # => #<Mock:0x12f287a&gt...
2006 Dec 02
3
Scoping expectations and more ...
...rubygems'' require ''mocha'' require ''stubba'' class BTest < Test::Unit::TestCase # This test demonstrates how you can call a function that calls a mocked # object more than one time in the same test def test_m_call b = B.new begin A.any_instance.expects(:m).with(5).returns(6) assert_equal 6, b.m(5) end begin A.any_instance.expects(:m).with(9).returns(10) assert_equal 10, b.m(9) end end # This test demonstrates how to mock objects where the method is called # more than one time with different parameters...
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: test_b NoMethodError: undefined method `method_n...
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 being tested (in this case, a test of a Rails controller''s ''create...
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)
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.", flash[:notice] end I expected that the stub would help my test pass but instead it failed and the failure included the following: test/functional/store_c...
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 create with invalid params re-renders the ''new'' template F...
2011 Aug 04
3
#create tests fail when I add FriendlyId to my model
...te'' test/functional/brands_controller_test.rb:27:in `test_create_valid'' Here''s the relevant line in brand.rb: has_friendly_id :name, :use_slug => true, :approximate_ascii => true Here''s the test (using mocha): def test_create_valid Brand.any_instance.stubs(:valid?).returns(true) post :create assert_redirected_to brand_url(assigns(:brand)) end Here''s the fixture: one: name: MyString image_file_name: MyString image_content_type: MyString image_file_size: 1 image_updated_at: 2011-08-03 10:49:03 tease: MyTe...
2007 Jun 12
4
advice on new failures with 0.5.0
...o 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 0.4.0, but with 0.5.0 it looks like Mocha is differentiating between a method call with parenthesis to one without parenthesis. I don''t really understand how this could happen, nor the logic in why parenthesis should be re...
2006 Oct 17
4
Mocking the rendering of a Rails template
...#39;t setup response.template to be an instance of ActionView::Base until after the controller processing is called. I have no idea what is going on with the second attempt. It''s hitting a mock, but I have no idea where that mock is coming from. Logically, it probably is coming from the any_instance magic, but in that case it should be expecting the render_file call. Thoughts? What am I doing wrong in the second case? Is there another way I could mock this? Any help would be greatly appreciated. -Bryan -- http://www.MyCongress.org/ -- coming soon
2006 Aug 14
14
A mock which extends rather than replaces a class?
While running tests, we would like to instrument some of the classes under test. We still want the classes to do exactly what they currently do, but we would like them to do more when running in the test environment. Clearly we can (and currently do) just extend the objects "on the fly" where necessary, but this is a bit messy - we''d like some centralised way to always
2006 Dec 15
5
Testing event driven Socket classes
Ok, here is the class, I want to Unit Test, its part of a large app and is based on EventMachine library. I want to mock the class TickServer ( i.e not stub it) . Since in actual scenario, you can''t do this on this class: @server = TickServer.new # will toss an exception at your face you must initialize the server like this: EventMachine.run {
2007 Nov 02
1
stub calls to an instance method before it is created?
I am desperately in need of functionality such as Mocha offers with their any_instance method. I see that there is an open ticket for a similar feature in RSpec, but it does not look like anything have been done. Is there any other way around this? https://rubyforge.org/tracker/index.php?func=detail&aid=6791&group_id=797&atid=3152 Here is what I am doing, and why I fee...
2006 Dec 21
4
Stubbing Kernel#open
...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 Object.any_instance.expects(:open).with(''http://www.google.com/search?q=california'').returns(stub(:body => body)) @response = GoogleSearch.new(''california'') assert_equal body, @response.body 1) Failure: test_new(GoogleSearchTest) [(eval):1:in `open''...
2007 Mar 04
4
Rails functional testing and Mocha
...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 beautiful and of course breaks down if there are multiple customers installed. So I mucked around a bit and came of with the following. It''s very ActiveRecord specific, but that''s what I''ve been dealing with.. I have a model Customer,...
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 May 28
3
Mocking objects to test Rails Controllers
Hi all, I''m currently considering the use of Mocha to aid in writing my Rails unit/functional tests. I currently have a Rails model which mixes in a series of methods to allow it to talk to an external service. The code is roughly of this form: class Person < ActiveRecord::Base include SMSNotifier def send_notification(message) send_sms(number,message) #
2006 Aug 15
7
Trying to Overide Class Object Methdos For Testing
Hi, I thought I had a handle on the whole singleton class thing but now I''m not too sure. I have a class definition: class ValidationMaster def validate(params) # complex and time consuming validation # returns true on success, raises Exception # on failure end end Okay, so when I''m running my functional tests within rails, I want to be able to control how
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: