it "renders index page" do
projects_user = FactoryGirl.create(:projects_user)
get :index, id: projects_user.id
expect(response).to render_template :index
end
这是我的控制器测试,当我运行它时说:验证失败:国家不能为空
我是stackoverflow和ruby soo的新手,我得到的任何帮助都很棒
FilesController:
class FilesController < ApplicationController
before_filter :authenticate_user!
def index
@pu = ProjectsUser.find(params[:id])
debugger
@files = UserFile.includes(:user).where(project_id: @pu.id)
end
def new
@file = UserFile.new
end
def create
@file = UserFile.new(file_params)
if @file.save
flash[:success] = "File successfully uploaded!"
redirect_to files_path
else
@files = UserFile.includes(:user).where(user_id: current_user.id)
render :index
end
end
def destroy
@file = UserFile.find(params[:file_id])
if @file.destroy
flash[:success] = "File successfully deleted!"
redirect_to files_path
else
flash[:error] = "File could not be deleted."
redirect_to files_path
end
end
protected
def file_params
params.require(:user_file).permit(:file, :user_id, :created_at, :updated_at, :project_id)
end
end
国家控制员:
class CountriesController < ApplicationController
def states
@states = State.where(country_id: params[:id]).all
return render :json => {success: true, status: 200, data: @states}
end
end
国家模式:
class Country < ActiveRecord::Base
validates :name, presence: true
has_many :states
has_many :users
has_and_belongs_to_many :projects
validates :name, presence: true
end
国家工厂:
require 'ffaker'
FactoryGirl.define do
factory :country do
name {FFaker::AddressUS.country}
end
end
输出错误:
ActiveRecord::RecordInvalid:
Validation failed: Countries can't be blank
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib /factory_girl/configuration.rb:18:in `block in initialize'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluation.rb:15:in `[]'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluation.rb:15:in `create'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:9:in `tap'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory.rb:42:in `run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:28:in `run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:5:in `association'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluator.rb:33:in `association'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute/association.rb:19:in `block in to_proc'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluator.rb:77:in `instance_exec'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluator.rb:77:in `block in define_attribute'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:56:in `get'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluation.rb:12:in `object'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory.rb:42:in `run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:28:in `run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/controllers/files_controllers_spec.rb:31:in `block (3 levels) in <top (required)>'
更新:
项目工厂,项目has_and_belongs_to_many国家
require 'ffaker'
FactoryGirl.define do
factory :project do
sponsor_name "John Pare"
title "Anea"
start_date Date.new(2016, 5, 7)
end_date Date.new(2017, 3, 6)
description {FFaker::Lorem.paragraph}
number_of_sites_planned "5"
estimated_enrollment "24"
feasibility_questionnaire File.open("#{Rails.root}/spec/internal_spec_files/app.pdf")
association :disease_type
association :disease_condition
association :user
association :project_category
association :project_subcategory
after(:create) do |m|
m.countries << build(:country)
end
end
端
答案 0 :(得分:0)
问题出在
之内 projects_user = FactoryGirl.create(:projects_user)
创建代码(您定义此工厂的地方)未设置国家/地区,这就是您收到错误的原因。