Aqui va mi plan.rb completo con todos los metodos (esta dentro de /spec/features/integration):
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'plan', type: 'feature' do
# Test suite for GET /plan
describe 'Visit /admin/plans' do
# make HTTP get request before each example
before(:all) do
@plan = []
3.times { @plan << create(:plan) }
end
before do
admin = create(:user_admin)
signin(admin.cui, admin.password)
visit '/admin/plans'
end
it 'return plans', js: true do
# Note `json` is a custom helper to parse JSON pages
expect(page.body).not_to be_empty
@plan.each do |plan|
expect(page).to have_content(plan.description.to_s)
end
end
it 'returns status code 200' do
expect(page).to have_http_status(:ok)
end
end
# Test suite for GET /plan/:id
describe 'Visit /admin/plan/:id' do
before do
admin = create(:user_admin)
signin(admin.cui, admin.password)
end
context 'When the plan exists' do
it 'Returns the plan' do
plan = create(:plan)
visit("/admin/plans/#{plan.id}")
expect(page).to have_content(plan.description)
end
it 'returns status code 200' do
expect(page).to have_http_status(:ok)
end
end
context 'when the record does not exist' do
it 'returns status code 404' do
visit('/admin/plans/100')
expect(page).to have_http_status(:not_found)
end
end
end
# Test suite for New plan
describe 'New /plan' do
context 'New plan' do
before do
admin = create(:user_admin)
signin(admin.cui, admin.password)
end
it 'create a plan' do
visit '/admin/plans/new'
fill_in('plan_description', with: 'Code1')
# select('plan_gender', :from=>'Masculino')
fill_in('plan_minimum_age', with: 1)
fill_in('plan_maximum_age', with: 100)
click_button('Crear')
expect(page).to have_content('correctamente')
expect(page).to have_http_status(:ok)
end
end
end
# Test suite for edit admin/plans
describe 'edit /admin/plans' do
context 'edit admin/plans' do
before do
admin = create(:user_admin)
signin(admin.cui, admin.password)
end
it 'edit a admin/plans' do
plan = create(:plan)
visit("/admin/plans/#{plan.id}/edit")
fill_in('plan_description', with: 'Code2')
click_button('Actualizar')
expect(page).to have_http_status(:ok)
expect(page).to have_content('Code2')
end
end
end
end
Estoy intentando realizar el test para el metodo delete de un controlador llamado 'Plan'. Lo que he intentado es lo siguiente:
it "deletes plan" do
plan = FactoryBot.create(:plan)
expect do
delete :destroy, params: { id: plan.id }
end.to change(Plan, :count).by(-1)
end
Y el error que obtengo es lo siguiente:
Failure/Error: delete :destroy, params: { id: plan.id } NoMethodError: undefined method `delete' for #<RSpec::ExampleGroups::Plan:0x00007fc26eaa46e0>
delete :destroy, params: { id: plan.id }
suena como que fuese un test de controlador. ¿que tipo es el que efectivamente quieres hacer?, type: :controller
? ¿funcionan otros métodos comoget
opost
? ¿funcionadelete
fuera del bloque deexpect
?