0

new_controller_spec.rb

require 'spec_helper'

describe NewController do

  describe 'actions' do 

    describe 'POST /franchise_opportunities' do  
     it 'renders v3/franchise_opportunities when domain id 5' do
        domain = double(Domain, mrdeliverycom?: true)
        controller.instance_variable_set(:@domain, domain)

        post :franchise_opportunities, name: 'v'
        response.should render_template('v3/franchise_opportunities')
      end
    end
  end
end

new_controller.rb

# -*- encoding : utf-8 -*-
class NewController < ApplicationController
 before_filter { @domain = ... }
 def franchise_opportunities

    t = Logger.new(STDOUT)
    t.debug '=============================================='
    t.debug @domain
    t.debug @domain.mrdeliverycom?
    t.debug '=============================================='

    render :layout=>'v3', :template=>'v3/franchise_opportunities' and return if @domain && @domain.mrdeliverycom?

    @errors={}
    @post = params.clone
    ...

 end
end

Log

    ==============================================
#<Domain id: 1, name: "hettingersawayn.org", email: "[email protected]", created_at: "2015-06-06 13:11:49", updated_at: "2015-06-06 13:11:49", domain_name: "ziemann.info8732", logo: nil, favicon: nil, slogan: nil, meta_title: nil, meta_keywords: nil, meta_description: nil, about_us: nil, smtp_server: "smtp.gmail.com", smtp_port: "587", smtp_username: nil, smtp_password: nil, smtp_authentication: "plain", smtp_tls: true, background: nil, source: "web", mobile_logo: nil, show_chef: false, ios_icon: nil, intro_text: nil>
    nil
    ==============================================

Seems @domain variable was set in spec method not overridden @domain variable was set in before_filter of the controller.

1 Answer 1

1

In fact the instance variable is set to the value that you provide in the example, but that happens before the before_filter executes, so it ends up being set again.

You could move the initialization from the before_filter into a method in the controller and stub that instead:

before_filter { @domain = get_domain }

...

protected

def get_domain
  ...
end

...

expect(controller).to receive(:get_domain).and_return("stubbed value")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.