I'm tripping on the Rspec test.
How do I write to test the association...
There are two things I don't understand.
1. I'm not sure how to associate with factorygirl in a one-to-many association (this time the bank model has many branches (model)).
2. I would like to guarantee that 20 branches linked to the bank will be created in the index test and 20 branches will be displayed on the first page, but it is not working at the moment...
Error Log
BankBranchesController
GET# index
21 Branch Data
Page 1 should be 20 (FAILED-1)
Failures:
1) BankBranchesController GET#index 21 branch data must be 20 on page 1.
Failure/Error: expect(assigns(:bank_branches).size).toeq Settings.per_page
expected —20
got:1
(compared using==)
# ./spec/controllers/bank_branches_controller_spec.rb: 14: in `block(4 levels) in <top(required)>'
app/model/bank.rb
has_many:bank_branches
app/model/bank_branch.rb
belongs_to:bank
factors/bank_branches.rb
FactoryGirl.definedo
factory:bank_branch, class:BankBranch do
sequence(:id)
sequence(:bank_id)
sequence(:code)
sequence(:name) {|i | "BankBranchName#{i}"}
sequence(:name_kana) {|i | "BankBranchNameKana#{i}"}
association:bank, factory::bank# I wonder if this is the association...
end
end
factors/bank.rb
FactoryGirl.definedo
factory:bank, class:Bank do
sequence(:id)
sequence(:code)
sequence(:name) {|i | "BankName#{i}"}
sequence(:name_kana) {|i | "BankNameKana#{i}"}
end
end
app/controllers/bank_branches_controller.rb
def index
@bank=Bank.eager_load(:bank_branches).find(params[:bank_id])
@query=bank.bank_branches.search(search_params)
@[email protected](params[:page]).per(Settings.per_page).order(code:asc)
end
spec/controllers/bank_branches_controller_spec.rb
describe BankBranchesController do
describe'GET#index'do
include_examples 'http_status200 ok'
context '21 branch data available' do
before do
create_list(:bank_branch,21)
end
It 'The first page should have 20 cases' do
get:index,params:{bank_id:1}
expect(assigns(:bank_branches).size).toeq Settings.per_page
end
It's 'The second page should be one case'
get:index,params:{bank_id:1,page:'2'}
expect(assigns(:bank_branches).size).toeq Settings.per_page
end
end
context 'data from bank_branches searched' do
subject do
get:index,params:{bank_id:1,q:condition}
assign(:bank_branches)
end
before do
@branch_data1=create(:bank_branch,bank_id:1,code:1,name:'Bank of Japan',name_kana:'Nihon Ginko')
If you specify Japan for context 'name_cont' do
let(:condition) {{name_cont:'Japan'}}
it { is_expected.to match_array([@branch_data1])}
end
end
end
$rake routes
bank_bank_branches GET/banks/:bank_id/bank_branches(.:format)bank_branches#index
As far as the log shows, I think the problem is that 21 bank_branch data have not been created on the create_list, but how can 21 be created...
I'm sorry to trouble you, but I'd appreciate it if you could let me know if you have any improvement measures.
Sorry for the inconvenience, but I appreciate your cooperation.
I'm sorry, but I solved it myself, so I'll let you know.
I think I made a mistake in associating the association in the first place.
factors/bank_branches.rb
FactoryGirl.definedo
factory:bank_branch, class:BankBranch do
sequence(:id)
sequence(:bank_id)
sequence(:code)
sequence(:name) {|i | "BankBranchName#{i}"}
sequence(:name_kana) {|i | "BankBranchNameKana#{i}"}
end
end
spec/controllers/bank_branches_controller_spec.rb
context 'If you have 21 branch data' do
before do
@bank=create(:bank)#Define associations here
create_list(:bank_branch,21,bank:@bank)
end
It 'The first page should have 20 cases' do
get:index,params:{bank_id:@bank}
expect(assigns(:bank_branches).size).toeq Settings.per_page
end
It's 'The second page should be one case'
get:index,params:{bank_id:@bank,page:'2'}
expect(assigns(:bank_branches).size).toeq1
end
end
I'm sorry to have troubled you.Thank you to those who came up with a solution.
© 2024 OneMinuteCode. All rights reserved.