【发布时间】:2019-10-10 01:40:34
【问题描述】:
我想用 RSpec 测试以下控制器
coupons_controller.rb:
class Api::V1::CouponsController < ApiController
def index
if params[:profile_id]
@coupons = Profile.find(params[:profile_id]).coupons
end
end
end
我想知道
1) 如何用FactoryBot创建工厂(spec/factories/profiles.rb, coupons.rb, coupon_profiles.rb)
2)spec/controllers/coupons_controllers.rb的写法:
关联
profile.rb
class Profile < ApplicationRecord
accepts_nested_attributes_for :coupon_profiles
end
优惠券.rb
class Coupon < ApplicationRecord
has_many :coupon_profiles
end
coupon_profile.rb
class CouponProfile < ApplicationRecord
belongs_to :coupon
belongs_to :profile
end
【问题讨论】:
标签: ruby rspec associations nested-attributes