Using FlexMock in a rails functional test
I have the following index action:
class ExpensesController < ApplicationController
def index()
@expenses = Expense.all
end
end
I want to mock the call to all in a functional test. I am using flexmock and have written the following test:
require 'test_helper'
require 'flexmock'
require 'flexmock/test_unit'
class ExpensesControllerTest < ActionController::TestCase
test "should render index" do
flexmock(Expense).should_receive(:all)开发者_如何学C.and_return([])
get :index
assert_response :success
assert_template :index
assert_equal [], assigns(:presentations)
end
end
The problem is the the last assertion fais with the following error message:
<[]> expected but was nil
I am confused what I am doing wrong. Should this not work?
Your test checks :presentations
:
assert_equal [], assigns(:presentations)
Did you mean :expenses
?
assert_equal [], assigns(:expenses)
Your controller is not setting @presentations
, so its value is nil
as reported.
精彩评论