3

I want to testing a private methods(not a action) of controller with rspec

class FooController < ApplicationController
  def some_methods( var )
    if  var 
      return 1
    else 
      return 2
    end
  end

  def some_action
    var = true
    r = some_methods(var)
    r
  end
end

rspec:

require 'spec_helper'

describe FooController do
   describe "GET index" do
      it "get get return value from some_methods" do
          @controller = TestController.new
          r =@controller.instance_eval{ some_action }  
          r.should eq 2
  end
end

This is my rspec code. However, the r is always 1 and I don't know how to pass paramater into some_action. How can I validate the real return values of some_methods using rspec way ? ( ex: r.should be_nil )

referenced but not work:

  1. Rspec, Rails: how to test private methods of controllers?

  2. How to spec a private method

Cœur
  • 37,241
  • 25
  • 195
  • 267
hey mike
  • 2,431
  • 6
  • 24
  • 29
  • What did you try, and what about the referenced questions didn't work for you? – Brandan Mar 26 '12 at 17:45
  • I try to write testing for the return value of `some_methods`. The referenced question 's solution can't determine the return value for `rspec`, ex: `r.should be_nil` – hey mike Mar 26 '12 at 18:01
  • So you've written some RSpec code to test the return value of `some_methods`? Can you post that? Why are you specifically trying to test the value of the local variable `r`? – Brandan Mar 26 '12 at 18:05
  • udpated. thanks. I'm trying to test the return value of some_methods. – hey mike Mar 26 '12 at 19:33

3 Answers3

4

I'm still a little confused. You shouldn't care about the return value of a controller action. Actions are supposed to render content, not return meaningful values. But I'll try to answer anyway.

If you're trying to make sure that FooController#some_action calls the private method #some_methods with a certain parameter, you can use #should_receive:

describe FooController do
  describe 'GET index' do
    it 'should get the return value from some_methods' do
      controller.should_receive(:some_methods).with(true).and_return(1)
      get :index
    end
  end
end

This example will fail if the controller never receives the message :some_methods, but it doesn't actually check the return value of the method #some_action (because that's almost never meaningful).

If you need to test the behavior of #some_methods, you should write separate tests for that using the techniques discussed in the articles you referenced.

Brandan
  • 14,735
  • 3
  • 56
  • 71
2

It looks like you've got a few different issues going on here:

  1. Why is r always returning 1? r is always 1 because you're calling some_action from insance_eval. some_action calls some_methods(true), and some_methods returns 1 if true is passed in. With the code you've supplied, some_action will always return 1

  2. I'm guessing these are just typo's, but your class name is FooController and the controller your testing in your rspec is TestController. Also, the some_methods function isn't marked as private.

  3. How to pass parameters, you should be able to call some_methods directly from your instance_eval block and pass in a parameter like a normal function call:

    r = @controller.instance_eval{ some_methods(false) }

    r.should eq 2

plainjimbo
  • 7,070
  • 9
  • 41
  • 55
0

Check into log with print/puts statement or use debugger 'rails-debug' to check value at run-time .

puts r = some_methods(var)

Vik
  • 5,931
  • 3
  • 31
  • 38
  • this is not solution, sorry. I think there is a `rspec` solution like `should be_nil` ... for example. – hey mike Mar 26 '12 at 11:01
  • 'rspec solution like should be_nil' is the check that you are putting into the testcases . But if you want to see the value of private method in controller , then 1) add puts/p/pp in controller and then check the test log. 2) use debugger , before method calling . Just take a look how debugger works . – Vik Mar 26 '12 at 11:13