ruby-on-railsruby-on-rails-4rspecfabrication-gem

Rspec 3 and Fabricator - controller spec, unable to create new record


I am trying to solve this without success. I am using RSpec with Faricator and can't get tests pass. The problem appears in create action. Update (with similar syntax) works just fine. From website form everything works correctly so I believe it is rather problem with specs. controller spec:

describe 'POST #create' do 
  before { set_current_admin admin }
  context 'admin users' do 
    context 'a successful create' do 
      before do 
        post :create, director: Fabricate.attributes_for(:director)
      end

      it 'saves new director object' do  
        #require 'pry'; binding.pry
        expect(Director.count).to eq(1)
      end

      it 'redirects to directors path' do 
        expect(response).to redirect_to directors_path
      end

      it 'sets a successful flash message' do 
        expect(flash[:success]).to eq('Reżyser został zapisany.')
      end
    end
  end  
end

set_current_admin admin method logs user as admin. I've checked using pry that logging is correct and logged user is an admin. Director controller:

class DirectorsController < ApplicationController
  before_action :set_director, only: [:edit, :update, :destroy, :show]
  before_action :admin_user, except: [:index, :show]

  def create
    @director = Director.new(directors_params)
    if @director.save
        flash[:success] = 'Reżyser został zapisany.'
        redirect_to directors_path
    else
        flash[:danger] = 'Coś poszło nie tak, spróbuj ponownie.'
        render :new
    end
  end

Do you know what might be solution for that?


Solution

  • Ok so specs are correct. I have used wrong Faker element - Faker::Hipster.paragraphs instead of Faker::Hipster.paragraph. First one generated an array and second a string.