如何使用has_and_belongs_to_many关联获取FactoryGirl模型属性?(How to get FactoryGirl model attributes with has_and_belongs_to_many association?)
我正在尝试使用
FactoryGirl.attributes_for(:trip)
关联获取FactoryGirl.attributes_for(:trip)
:countries
因为控制器测试失败 -:countries
缺少的:countries
:trip
属性):TripsController:
class TripsController < ApplicationController def create trip = Trip.new(create_params) if trip.save redirect_to trips_path, notice: 'Trip successfully created.' else redirect_to :back, alert: trip.errors.full_messages.join('<br>').html_safe end end def create_params params.require(:trip).permit(:end_date, :description, country_ids: [], countries: []) end end
RSpec TripsController测试:
describe TripsController do describe 'POST #create' do before { post :create, trip: attributes_for(:trip) } it { is_expected.to redirect_to trips_path } end end
旅行模型:
class Trip < ActiveRecord::Base # Associations has_and_belongs_to_many :countries #Validations validate :require_at_least_one_country private def require_at_least_one_country if country_ids.empty? && countries.count == 0 errors.add(:base, 'Please select at least one country') end end end
旅行工厂:
FactoryGirl.define do factory :trip do description { Faker::Lorem.sentence } end_date { DateTime.now + 1.day } after(:build) do |trip, evaluator| trip.countries << FactoryGirl.create(:country, :with_currencies) end end end
的Gemfile:
factory_girl_rails (4.5.0)
试过这个: http : //makandracards.com/jan0sch/11111-rails-factory-and-has_and_belongs_to_many ,但没用。
I'm trying to get
FactoryGirl.attributes_for(:trip)
with HABTM association:countries
because controller test fails -:countries
absent in:trip
attributes):TripsController:
class TripsController < ApplicationController def create trip = Trip.new(create_params) if trip.save redirect_to trips_path, notice: 'Trip successfully created.' else redirect_to :back, alert: trip.errors.full_messages.join('<br>').html_safe end end def create_params params.require(:trip).permit(:end_date, :description, country_ids: [], countries: []) end end
RSpec TripsController test:
describe TripsController do describe 'POST #create' do before { post :create, trip: attributes_for(:trip) } it { is_expected.to redirect_to trips_path } end end
Trip model:
class Trip < ActiveRecord::Base # Associations has_and_belongs_to_many :countries #Validations validate :require_at_least_one_country private def require_at_least_one_country if country_ids.empty? && countries.count == 0 errors.add(:base, 'Please select at least one country') end end end
Trip factory:
FactoryGirl.define do factory :trip do description { Faker::Lorem.sentence } end_date { DateTime.now + 1.day } after(:build) do |trip, evaluator| trip.countries << FactoryGirl.create(:country, :with_currencies) end end end
Gemfile:
factory_girl_rails (4.5.0)
Tried this: http://makandracards.com/jan0sch/11111-rails-factorygirl-and-has_and_belongs_to_many, but useless.
原文:https://stackoverflow.com/questions/32558059
最满意答案
您忘记了
main.go
的包前缀,并且您的函数未导出 ,这意味着它无法从其他包访问。 要导出标识符,请在名称的开头使用大写字母:utils.UtilsDoSomething()
一旦你有了
utils
前缀,你也可以删除Utils
的名字:utils.DoSomething()
如果你想从
utils
包中导入所有的东西到主应用程序的命名空间中,请执行以下操作:import . "cvs/user/project/utils"
之后,您可以直接使用
DoSomething
。You forgot the package prefix in
main.go
and your function is not exported, meaning it is not accessible from other packages. To export an identifier, use a capital letter at the beginning of the name:utils.UtilsDoSomething()
Once you have the
utils
prefix you can also drop theUtils
in the name:utils.DoSomething()
If you want to import everything from the
utils
package into the namespace of your main application do:import . "cvs/user/project/utils"
After that you can use
DoSomething
directly.
相关问答
更多-
'this'在npm包中未定义('this' undefined in npm package)[2023-05-16]
您应该将auth客户端创建为实例,即 var client = new Oauth2Client(... You should create the auth client as an instance, i.e. var client = new Oauth2Client(... -
因为在Javascript中,在其他函数中声明的函数是本地引用,并且在其父函数的范围之外不可见。 如果你想让你的updateSizeOptions函数全局可访问,你将需要在全局命名空间中为它指定一个引用,比如一个window属性: window.updateSizeOptions = updateSizeOptions; Because in Javascript, functions declared within other functions are local references, and ar ...
-
声明并定义了“未定义的引用”错误偶数函数(“Undefined reference to” error even function is declared and defined)[2022-11-02]
您将函数mysql_storage_init声明为static ,这意味着它们具有内部链接 - 这意味着它们对其他翻译单元不可见。 由于您需要从其他目标文件中调用它们,因此您应该通过在声明和定义中省略static关键字来声明它们具有外部链接 。 头文件中的原型可以选择将它们声明为extern ,但这不是必需的,因为默认情况下链接是外部的。 You declared your functions mysql_storage_init as static, meaning they have internal ... -
有几种方法可以解决这个问题,比如像renderItem那样在构造函数中放置renderItem ,或者您可以使用属性初始值设定项: renderItem = ({section, item}) => { return
} 现在this将指向该组件,并允许 ... -
您忘记了main.go的包前缀,并且您的函数未导出 ,这意味着它无法从其他包访问。 要导出标识符,请在名称的开头使用大写字母: utils.UtilsDoSomething() 一旦你有了utils前缀,你也可以删除Utils的名字: utils.DoSomething() 如果你想从utils包中导入所有的东西到主应用程序的命名空间中,请执行以下操作: import . "cvs/user/project/utils" 之后,您可以直接使用DoSomething 。 You forgot the p ...
-
Create or replace package pk2 as type c1 is ref cursor return employees%rowtype; type outrec is record ( var_num number, var1 varchar2(50), var2 varchar2(50)); type outrecset is table of outrec; function f_trans (c2 c1) return outre ...
-
“注意:未定义的变量”但变量在函数中声明(“Notice: Undefined variable” but variable is declared in a function)[2022-03-20]
您的变量在函数的本地范围内定义,永远不会在其外部访问。 除非您将其定义为全局。 http://php.net/manual/en/language.variables.scope.php 所以快速解决方案看起来像这样: function getUserData_FromSession() { global $userid; (...) } Your variable is defined in a local scope of the function and will never be ... -
如果查看startTimer主体上方的行,您将看到go编译器的特殊指令: //go:linkname stopTimer time.stopTimer 从编译命令文档 //go:linkname localname importpath.name // go:linkname指令指示编译器使用“importpath.name”作为源代码中声明为“localname”的变量或函数的目标文件符号名称。 由于此伪指令可以破坏类型系统和包模块化,因此仅在导入“不安全”的文件中启用它。 If you look o ...
-
我假设你实际上想要粒子是粒子的数组 注1:添加的代码仅 run到particles数组 注2:将particle更改为Particle - 仅用于“约定”原因 var particles; function setup() { createCanvas(window.innerWidth, window.innerHeight); particles = []; for (var i = 0; i < 5; i++) { particles.push(new Part ...
-
在* .h中声明的未定义引用已在另一个* .h中声明(Undefined reference to declared in *.h already declared in another *.h)[2022-06-24]
在graph.h中包含list.h,并且只在graph.c中包含graph.h Include list.h in graph.h,and only include graph.h in graph.c