mocha

Testing methods called on yielded object

I have the following controller test case: def test_showplain Cleaner.expect(:parse).with(@somecontent) Cleaner.any_instance.stubs(:plainversion).returns(@returnvalue) post :showplain, {:content => @somecontent} end This works fine, except that I want the "stubs(:plainversion)" to be an "expects(:plainversion)". Here's the co...

Is there a way to undo Mocha stubbing of any_instance in Test::Unit

Much like this question, I too am using Ryan Bates's nifty_scaffold. It has the desirable aspect of using Mocha's any_instance method to force an "invalid" state in model objects buried behind the controller. Unlike the question I linked to, I'm not using RSpec, but Test::Unit. That means that the two RSpec-centric solutions there won't...

Weird error when trying to test method with argument in Mocha. Is it a bug or is it me?

Hi there. It's rather hard to find any documentation on Mocha, so I'm afraid I'm totally at sea here. I have found a problem with stubbing methods that pass arguments. So for instance if I set up a class like this: class Red def gets(*args) @input.gets(*args) end def puts(*args) @output.puts(*args) end def initial...

Rails Controller Tests for Captcha using Shoulda, Factory Girl, Mocha

Can someone provide a strategy/code samples/pointers to test Captcha validations + Authlogic using Shoulda, Factory Girl and Mocha? For instance, my UsersController is something like: class UsersController < ApplicationController validates_captcha ... def create ... if captcha_validated? # code to deal with user attributes end ....

Mocha Mock Carries To Another Test

I have been following the 15 TDD steps to create a Rails application guide - but have run into an issue I cannot seem to resolve. For the functional test of the WordsController, I have the following code: class WordsControllerTest < ActionController::TestCase test "should get learn" do get 'learn' assert_response :success ...

RSpec on Controller and Stubbing

I am pretty new to using rspec and am trying to write my tests for my controllers. I have this controller (I am using mocha for stubbing): class CardsController < ApplicationController before_filter :require_user def show @cardset = current_user.cardsets.find_by_id(params[:cardset_id]) if @cardset.nil? flash[:notice]...

Force controller to use current_user with mocking

I am trying to specify in my RSpec tests that my controller should use current_user.projects.find() instead of Project.find() I am using the Mocha mocking framework and was trying something like this: controller.current_user.projects.expects(:find).returns(@project) I have already mocked out controller.stubs(:current_user).returns(@pr...

Expectation for find not working, but expectation for find_by_id is

I have this controller code: # GET /cardsets/1 def show @cardset = current_user.cardsets.find_by_id(params[:id]) end And this RSpec test code (mocking with Mocha): # GET Show context "on get to show" do it "should assign cardset" do @cardset = Factory(:cardset) @profile = @cardset.profile @profile.cardsets.expects(:f...

Stub with Rspec/Mocha in Cucumber scenarios

Hi. I am using Cucumber as the BDD framework with rspec/mocha mocking. Ideally we would not mock/stub behavior in cucumber specs; however the scenario is exceptional here. To give you the brief idea of problem here; I have two features product feature and cart feature. Cart feature is currently mocking some of the product fetch from 3 ...

Mocha expectation on association build call failing

I have this example: # GET New context "on get to new" do it "should assign cardset" do @profile.cardsets.expects(:build).once.returns(Factory.stub(:cardset)) get :new assigns[:cardset].should_not be_nil end end To test this method: # GET /cardsets/new def new @cardset = current_user.cardsets.build end I am trying...

Mock methods that receives a block as parameter

I have a scenario more or less like this class A def initialize(&block) b = B.new(&block) end end I am unit testing class A and I want to know if B#new is receiving the block passed to A#new. I am using Mocha as mock framework. Is it possible? ...

Testing two different expectations with mocking

I've recently just added Devise to my first Rails3 app, and I'm having a bit of trouble with the controller tests. I'm testing the User controller class, which is the same model that Devise uses. So at the beginning of my spec I have this: before(:each) do sign_in @user = Factory.create(:user) end Now I can get the test passing wit...

Getting Rails 3 Generators with Rspec 2 and Mocha

I've followed all of the steps that I've been able to find online for configuring Rails 3 with Rspec 2 and Mocha. In my Gemfile: group :development do gem 'rails3-generators' gem "rspec", '>= 2.0.0.beta.19' gem "rspec-rails", '>= 2.0.0.beta.19' end group :test do gem "faker" gem "rspec", '>= 2.0.0.beta.19' gem "rspec-rails"...

Mocking an external API

I'm new to testing strategies and mocking, and I'm having a tough time figuring out how to mock a call to an external service. I'm sure it's something easy I'm missing, I just don't know what exactly. I'm using the Braintree gem to charge for subscription services through the Braintree gateway, and I wanted to mock the Customer create ...

Rails Faking a Route

To be specific, I'm trying to get ActionController::Routing::Routes.recognize_path to recognize a route that is not in routes.rb, for testing purposes. Is it possible to somehow mock or dynamically add a route? I'm using Rspec with Mocha. ...

Why am I getting a NoMethodError when I use Mocha 0.9.8 with Test::Unit 2.1.1

Has anyone seen this? Here's the error: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -rrubygems -e "require 'redgreen'" -I.:lib:test -rubygems -e "['test/unit', 'test/test_authentication.rb'].each { |f| require f }" | /Library/Ruby/Gems/1.8/gems/autotest-4.3.2/bin/unit_diff -u Load/Library/Ruby/Gems/1.8/gems/moch...

Using MochaUI windows tabs for MUI.Panels?

When using MochaUI to open a new floating container (from the demo menu File>Tests>any), a new tab appears in the "Dock" bar, and when one click this tab the floating window minize/restore. What I'd like to use is to use those tabs but having the containers (maximized) into a panel. For example, I click File>Open>Something_1111, and a ...

Mocking authlogic from shoulda with mocha

Hi, I am having trouble mocking authlogic from shoulda. I have the following test fixture: class HomeControllerTest < ActionController::TestCase context "on GET to index" do setup do activate_authlogic UserSession.stubs(:current_user).returns( user_session ) get :index end should respond_with :success ...

Why doesn't this test in the Diaspora app fail?

From http://github.com/diaspora/diaspora/blob/master/spec/models/profile_spec.rb describe Profile do before do @person = Factory.build(:person) end describe 'requirements' do it "should include a first name" do @person.profile = Factory.build(:profile,:first_name => nil) @person.profile.valid?.should be false ...

Mocking/Stubbing an Application Controller method with Mocha (Using Shoulda, Rails 3)

While writing functional tests for a controller, I came across a scenario where I have a before_filter requesting some information from the database that one of my tests requires. I'm using Factory_girl to generate test data but I want to avoid hitting the database when its not explicitly needed. I'd also like to avoid testing my before_...