用FactoryGirl创建的模型在控制器中不可用

用FactoryGirl创建的模型在控制器中不可用

本文介绍了用FactoryGirl创建的模型在控制器中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用RSpec和Capybara编写一些功能测试,想知道为什么我的gon对象尽管在我的控制器中设置了却总是空的:

I'm trying to write some feature tests with RSpec and Capybara and wonder, why my gon object is always empty allthough it was set in my controller:

app / controllers / timetrackings_controller.rb

class TimetrackingsController < ApplicationController
  include ApplicationHelper

  before_action :authenticate_user!
  before_action :set_timetracking, only: [:update, :destroy]

  # GET /timetrackings
  def index
    projects = Project.all_cached.select('id, name, category, number, customer_id').includes(:customer).where(:archived => false).order(:number)
    gon.projects = group_by_customer(projects).to_h
    gon.services = Service.all_cached.select('id, name').where(:archived => false).order('LOWER(name)')
  end

  ...

现在,我想知道为什么这些数据没有呈现到我的视图中(用 save_and_open_page //<![CDATA [window.gon = {}; gon.projects = {}; gon.services = []; / /]]> ),所以我只是尝试在测试文件中获取gon值:

Now I've wondered why this data doesn't get rendered into my view (debugged with save_and_open_page: //<![CDATA[ window.gon={};gon.projects={};gon.services=[]; //]]>), so I just tried to get the gon values in my test file:

require 'spec_helper'

describe 'the timetracking page', :js => true do

  before :each do
    switch_to_subdomain('test')

    @project = FactoryGirl.create(:project)
    @service = FactoryGirl.create(:service)

    user = FactoryGirl.create(:user)
    login_as(user, :scope => :user)

    visit '/timetracking'

    save_and_open_page
  end


  it 'renders the react component' do
    expect(page).to have_selector '#timetracking-form'
  end

  it 'allows me to create a new timetracking' do
    within('#timetracking-form') do
      fill_in 'duration', :with => '2'

      puts '########'
      puts Gon.all_variables
      puts Project.all.to_json
      puts Service.all.to_json
      puts '########'

      find_field('project_id').find("option[value='#{@project.id}']").click
    end
    click_button '.ei-icon-check'
  end

end

但输出仅为:

########
{}
[{"id":2,"number":"2","name":"project2","description":"Some notes","archived":false,"customer_id":2,"created_at":"2015-11-30T16:05:40.160+01:00","updated_at":"2015-11-30T16:05:40.160+01:00","rate_type":null,"hourly_rate":null,"service_rates":null,"budget_type":null,"budget_rate":null,"category":"A","deadline":null}]
[{"id":2,"name":"service2","description":"Some notes","archived":false,"created_at":"2015-11-30T16:05:40.173+01:00","updated_at":"2015-11-30T16:05:40.173+01:00","billable":null,"hourly_rate":null}]
########

那是我的 spec_helper.rb

require 'devise'

# Setup Capybara
require 'capybara/rspec'
require 'capybara/poltergeist'
Capybara.always_include_port = true
Capybara.javascript_driver = :poltergeist

# Use SimpleCov for code coverage
require 'simplecov'
require 'simplecov-shield'
SimpleCov.start 'rails'
SimpleCov.formatters = [
  SimpleCov::Formatter::HTMLFormatter,
  SimpleCov::Formatter::ShieldFormatter
]

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'shoulda-matchers'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

module ControllerMacros
  def attributes_with_foreign_keys(*args)
    FactoryGirl.build(*args).attributes.delete_if do |k, v|
      ['id', 'type', 'foreign_id', 'foreign_type', 'created_at', 'updated_at'].member?(k)
    end
  end
end

RSpec.configure do |config|

  config.use_transactional_fixtures = false
  config.use_instantiated_fixtures  = false
  config.mock_with :rspec

  # Use FactoryGirl for fixtures
  config.include FactoryGirl::Syntax::Methods

  # Auto-detect spec types
  config.infer_spec_type_from_file_location!

  # Insert devise helpers in controller specs
  config.include Devise::TestHelpers, type: :controller
  config.include Warden::Test::Helpers
  config.extend ControllerMacros, type: :controller
  config.include ControllerMacros


  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = 'random'

  config.before(:suite) do

    Warden.test_mode!

    # Clean all tables to start
    DatabaseCleaner.clean_with :truncation

    # Use transactions for tests
    DatabaseCleaner.strategy = :transaction

    # Truncating doesn't drop schemas, ensure we're clean here, app *may not* exist
    Apartment::Tenant.drop('test') rescue nil

    # Create the default tenant for our tests
    Account.create!(name: 'Test', domain: 'test', email: '[email protected]')

  end

  config.before(:each) do

    # Start transaction for this test
    DatabaseCleaner.start

    # Switch into the default tenant
    Apartment::Tenant.switch! 'test'

    # Use Timecop to freeze times on time-critical tests
    Timecop.return

  end

  config.after(:each) do

    # Reset tentant back to `public`
    Apartment::Tenant.reset

    # Rollback transaction
    DatabaseCleaner.clean

  end

end



更新



好吧,我认为问题出在其他地方:

Update

OK, I think the problem lies elsewhere:

用FactoryGirl创建模型后(例如 FactoryGirl。 create(:project)),则该记录在控制器中不可用。如果我写类似

After creating models with FactoryGirl (like FactoryGirl.create(:project)), the records aren't available in the controller. If I write something like

@foo = Project.all.to_json

在我的控制器中并想在我的视图中显示此数据,我只得到 [] (在调用后 save_and_open_page )。

in my controller and want to display this data in my view, I just get [] (after calling save_and_open_page).

尽管 FactoryGirl.create 写道数据直接发送到数据库?为什么我的控制器方法中没有数据?

I though FactoryGirl.create writes data directly to the DB? Why isn't the data available in my controller methods?

添加

config.before(:each, :js => true) do
  DatabaseCleaner.strategy = :truncation
end

解决了此问题,但现在我得到了:

solves this problem, but now I get:

 Failure/Error: user = FactoryGirl.create(:user)
     ActiveRecord::RecordNotUnique:
       PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_users_on_lastname"
       DETAIL:  Key (lastname)=(Kautzer) already exists.
       : INSERT INTO "users" ("email", "initial", "firstname", "lastname", "encrypted_password", "archived", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"

我认为数据库已清理每次测试后?

I thought the database gets cleaned after each test?

无视,最后一个问题是我的数据库设置有误。 / p>

Disregard, the last problem was a mistake in my database setup.

推荐答案

添加

config.before(:each, :js => true) do
  DatabaseCleaner.strategy = :truncation
end

到我的 spec_helper.rb 解决了这个问题:)

to my spec_helper.rb solves the problem :)

这篇关于用FactoryGirl创建的模型在控制器中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:42