【问题标题】:NameError: undefined local variable or method `user'NameError:未定义的局部变量或方法“用户”
【发布时间】:2016-04-13 12:35:31
【问题描述】:

我正在使用 RoR 编写应用程序,使用 gem Devise 进行用户身份验证。我正在尝试测试用户登录应用时的行为并出现下一个错误:

User::TransactionsController when logged in when its own record GET #show assigns the requested instance as @instance
     Failure/Error: let(:transaction) { FactoryGirl.create(:transaction, user_id: user.id) }

     NameError:
       undefined local variable or method `user' for #<RSpec::ExampleGroups::UserTransactionsController::WhenLoggedIn::WhenItsOwnRecord::GETShow:0x00000004d77220>

我的测试开始于:

RSpec.describe User::TransactionsController, type: :controller do
  render_views

  before { sign_in FactoryGirl.create :user }

  let(:transaction_category) { FactoryGirl.create(:transaction_category) }
  let(:transaction) { FactoryGirl.create(:transaction, user_id: user.id) }
  ......
end

我的工厂:

FactoryGirl.define do
  factory :transaction do
    date '2016-01-08'
    comment 'MyString'
    amount 1
    transaction_category

    trait :invalid do
      amount nil
    end
  end
end

我的 TransactionsController 看起来像:

class User::TransactionsController < ApplicationController
  before_action :authenticate_user!
  before_action :find_transaction, only: [:show, :edit, :destroy, :update]

  def new
    @transaction = current_user.transactions.build
  end

  def show
  end

  def create
    @transaction = current_user.transactions.build(transaction_params)
    if @transaction.save
      redirect_to user_transaction_url(@transaction)
    else
      render :new
    end
  end

  def index
    @transactions = current_user.transactions
  end

  def edit
  end

  def destroy
    @transaction.destroy
    redirect_to user_transactions_url
  end

  def update
    if @transaction.update(transaction_params)
      redirect_to user_transaction_url
    else
      render :edit
    end
  end

  private

  def transaction_params
    params.require(:transaction).permit(:amount, :date, :comment,
                                        :transaction_category_id)
  end

  def find_transaction
    @transaction = current_user.transactions.find(params[:id])
  end
end

谢谢!

【问题讨论】:

  • 用户变量丢失。创建一个: let(:user) { FactoryGirl.create :user } 并在 before 块中使用它。
  • @Šaras,谢谢!那行得通。但在我的第一个变体中,它不是由before { sign_in FactoryGirl.create :user } 定义的吗?
  • 在before 块中,您正在创建另一个用户,并且它的变量没有保存在任何地方。基本上,每次执行FactoryGirl.create :user 时,您都在创建一个新的唯一用户。因此,如果您想让一个用户附加交易并以他的身份登录,您需要在let 块中创建一个,以便它可以用于登录和新交易创建。 @verrom
  • @Šaras,非常感谢!! :)

标签: ruby-on-rails ruby-on-rails-4 rspec rspec-rails


【解决方案1】:

你需要定义用户

RSpec.describe User::TransactionsController, type: :controller do


render_views
  user = FactoryGirl.create( :user )
  before { sign_in( user ) }

  let(:transaction_category) { FactoryGirl.create(:transaction_category) }
  let(:transaction) { FactoryGirl.create(:transaction, user_id: user.id) }
  ......
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 2016-02-14
    • 2016-04-08
    • 1970-01-01
    相关资源
    最近更新 更多