Rspec。删除操作不适用于 let()
Rspec. Delete action doesn't work with let()
我已经 Rspec 通过访问页面测试控制器操作。
当我通过 let() 为 create
和 edit
操作声明新对象时 - 一切正常。但是对于 delete
它不起作用,我必须声明实例变量才能通过。
我的目标是删除删除操作中的实例变量声明。
它阻止我完成重构,我想知道为什么会这样?
也许它与 Capybara scope 有某种关系。
失败的测试看起来像:
Failure/Error: it {should have_content('delete')}
expected #has_content?("delete") to return true, got false
和
Failure/Error: expect{click_link('delete')}.to change(Crew, :count).by(-1)
Capybara::ElementNotFound:
Unable to find link "delete"
我的整个测试
require 'spec_helper'
describe 'CrewPage' do
subject {page}
let(:user) {FactoryGirl.create(:user)}
let(:crew) {Crew.create(name: name, secondname: secondname, rate: rate)}
let(:name) {'Name'}
let(:secondname) {'First'}
let(:rate) {1000}
before {sign_in(user)}
#==============================New page===========>>
describe 'new crew member page' do
before {visit new_crew_path}
it {should have_title('New member')}
let(:submit) {"Create"}
context 'invalid creation' do
it 'should not create crew member' do
expect{click_button submit}.not_to change(Crew, :count)
end
end
context 'valid crew member creation' do
before do
fill_in 'Name', with: name
fill_in 'Secondname', with: secondname
fill_in 'Rate', with: rate
end
it 'should create crew member' do
expect{click_button submit}.to change(Crew, :count).by(1)
end
end
end
#==============================Show page===========>>
describe 'show page' do
before {visit crew_path(crew)}
it {should have_title("#{crew.combine_names} profile")}
end
#==============================Edit page===========>>
describe 'edit page' do
let(:reload_n) {name*2}
let(:reload_sn) {secondname*2}
let(:reload_r) {rate*2}
before {visit edit_crew_path(crew)}
it {should have_title('Edit panel')}
context 'successfull edit' do
before do
fill_in 'Name', with: reload_n
fill_in 'Secondname', with: reload_sn
fill_in 'Rate', with: reload_r
click_button('Save')
end
it {should have_content(reload_n)}
it {should have_content(reload_sn)}
it {should have_content(reload_r)}
end
end
#==============================Delete action===========>>
describe 'delete action from index page' do
before do
@crew = Crew.create(name: name, secondname: secondname, rate: rate)
visit root_path
end
it {should have_content('delete')}
it 'should delete crew member' do
expect{click_link('delete')}.to change(Crew, :count).by(-1)
end
end
end
let
块不会被执行,除非它在测试本身中被调用。因为您在 visit_page
之前没有使用这个变量,所以不会创建船员模型,因此它不会显示在水豚正在玩的页面上。如果你想让它在每次测试之前执行,你必须调用 let!
代替:
let!(:crew) { Crew.create(name: name, secondname: secondname, rate: rate)}
我已经 Rspec 通过访问页面测试控制器操作。
当我通过 let() 为 create
和 edit
操作声明新对象时 - 一切正常。但是对于 delete
它不起作用,我必须声明实例变量才能通过。
我的目标是删除删除操作中的实例变量声明。
它阻止我完成重构,我想知道为什么会这样? 也许它与 Capybara scope 有某种关系。
失败的测试看起来像:
Failure/Error: it {should have_content('delete')}
expected #has_content?("delete") to return true, got false
和
Failure/Error: expect{click_link('delete')}.to change(Crew, :count).by(-1)
Capybara::ElementNotFound:
Unable to find link "delete"
我的整个测试
require 'spec_helper'
describe 'CrewPage' do
subject {page}
let(:user) {FactoryGirl.create(:user)}
let(:crew) {Crew.create(name: name, secondname: secondname, rate: rate)}
let(:name) {'Name'}
let(:secondname) {'First'}
let(:rate) {1000}
before {sign_in(user)}
#==============================New page===========>>
describe 'new crew member page' do
before {visit new_crew_path}
it {should have_title('New member')}
let(:submit) {"Create"}
context 'invalid creation' do
it 'should not create crew member' do
expect{click_button submit}.not_to change(Crew, :count)
end
end
context 'valid crew member creation' do
before do
fill_in 'Name', with: name
fill_in 'Secondname', with: secondname
fill_in 'Rate', with: rate
end
it 'should create crew member' do
expect{click_button submit}.to change(Crew, :count).by(1)
end
end
end
#==============================Show page===========>>
describe 'show page' do
before {visit crew_path(crew)}
it {should have_title("#{crew.combine_names} profile")}
end
#==============================Edit page===========>>
describe 'edit page' do
let(:reload_n) {name*2}
let(:reload_sn) {secondname*2}
let(:reload_r) {rate*2}
before {visit edit_crew_path(crew)}
it {should have_title('Edit panel')}
context 'successfull edit' do
before do
fill_in 'Name', with: reload_n
fill_in 'Secondname', with: reload_sn
fill_in 'Rate', with: reload_r
click_button('Save')
end
it {should have_content(reload_n)}
it {should have_content(reload_sn)}
it {should have_content(reload_r)}
end
end
#==============================Delete action===========>>
describe 'delete action from index page' do
before do
@crew = Crew.create(name: name, secondname: secondname, rate: rate)
visit root_path
end
it {should have_content('delete')}
it 'should delete crew member' do
expect{click_link('delete')}.to change(Crew, :count).by(-1)
end
end
end
let
块不会被执行,除非它在测试本身中被调用。因为您在 visit_page
之前没有使用这个变量,所以不会创建船员模型,因此它不会显示在水豚正在玩的页面上。如果你想让它在每次测试之前执行,你必须调用 let!
代替:
let!(:crew) { Crew.create(name: name, secondname: secondname, rate: rate)}