2016-04-13 15 views
1

Kullanıcı kimlik doğrulaması için gem Devise kullanarak RoR kullanarak bir uygulama yazıyorum.NameError: tanımlanmamış yerel değişken veya yöntem "kullanıcı"

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 

Benim fabrika:

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

    trait :invalid do 
     amount nil 
    end 
    end 
end 

Benim TransactionsController

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> 

Benim testlerde

ile başlar: o gelecek hatayı uygulamasında oturum açar ve ne zaman kullanıcı davranışını test çalışıyorum gibi görünüyor:

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 

Teşekkürler!

+1

Değişken eksik tanımlamanız gerekir. Bir tane oluşturun: let (: user) {FactoryGirl.create: user} ve bloktan önce de kullanın. – ksarunas

+0

@ Šaras, teşekkürler! Thats çalıştı. Ama benim ilk varyasyonumda 'before {sign_in FactoryGirl.create: user}' ile tanımlanmış değil mi? – verrom

+1

'before' bloğunda başka bir kullanıcı yaratıyorsunuz ve değişken herhangi bir yerde kaydedilmiyor. Temel olarak, her zaman 'FactoryGirl.create: user' çalıştırdığınızda yeni ve benzersiz bir kullanıcı yaratıyorsunuz. Böylece eğer ona bağlı bir kullanıcısı varsa ve aynı zamanda onunla da oturum açmış olursanız, hem oturum açma hem de yeni işlem oluşturma için kullanılabilecek şekilde 'let 'bloğunda bir tane oluşturmanız gerekir. @verrom – ksarunas

cevap

0

Sen kullanıcıya kullanıcıya

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 
İlgili konular