2016-12-22 43 views
0

控制器實例變量我有簡單的控制器:不能得到使用RSpec

class CreditLogsController < BaseController 

    def show 
    @account = Account.find_by_email(Base64.urlsafe_decode64(params[:id])) 
    end 
end 

,這裏是規範它:

require 'rails_helper' 

describe CreditLogsController, type: :controller do 

    describe 'GET #show' do 
    it 'responds with 200' do 
     create(:account) 

     get :show, params: {id: Base64.urlsafe_encode64('[email protected]')}, format: :html 
     puts "############# #{controller.instance_variable_get(:account)}" 
     expect(assigns(:account)).to eql('[email protected]') 
    end 
    end 

end 

的問題是,account在規範總是nil,在覆蓋來自控制器的文件代碼分配值爲@account顯示爲未涵蓋,並且controller.instance_variable_get(:account)產生了一個錯誤:

`account' is not allowed as an instance variable name.

我在其他規範中有類似的代碼,工作正常,所以我做錯了什麼?

+0

您是否使用FactoryGirl創建帳戶?你正在比較一個對象'@ account'和一個字符串''tes1 @ test.com''?它應該是'expect(指定(:account).email).to eql('[email protected]')' – Thanh

+0

似乎代碼指針未達到show action方法 –

回答

-1

如錯誤所示,這是實例變量的錯誤名稱。他們必須從@開始,所以描述它們的符號和字符串也是如此。

您必須使用:

controller.instance_variable_get(:@account) 

controller.instance_variable_get('@account') 

測試(expect(assigns(:account))...)的其餘部分將無法達成,因爲instance_variable_get(:account)將引發一個NameError例外。

+0

如果我使用controller.instance_variable_get(: @account)我有同樣的問題,值是零 – maki

+0

@maki所以你的'find_by_email'不工作,我看不出爲什麼會這樣。做一些調試,在那裏放一個'binding.pry'。我們無法爲您解決所有問題。 – meagar

+1

你是不正確的,問題不在find_by_email,例如,如果我只是分配一些值@account =「測試」它仍然無規範 – maki