【发布时间】:2019-01-13 02:15:30
【问题描述】:
我正在尝试为我的 user_stocks_controller 的销毁操作编写 rspec 测试,但出现错误:
UserStocksController DELETE #destroy 删除库存失败/错误: 删除:销毁,参数:{ id: user_stock.id }
ActionController::UrlGenerationError: 没有路由匹配 {:action=>"destroy", :controller=>"user_stocks", :id=>nil}
我已经读到存在问题,因为我没有提供库存的 id。我试图以多种不同的方式对其进行更改,但我的想法不正确。
这是我的规格:
let(:user) { User.new(email: 'test@example.com', password: 'password') }
before do
sign_in(user)
end
describe 'DELETE #destroy' do
let(:stock) { Stock.new_from_lookup('GS') }
let(:user_stock) { UserStock.create(user: user.id, stock: stock) }
it 'deletes stock' do
expect do
delete :destroy, params: { id: user_stock.id }
end.to change(UserStock, :count).by(-1)
expect(response).to have_http_status(redirect)
expect(flash[:notice]).to eq 'Stock successfully removed'
end
end
user_stocks_controller 中的销毁方法:
def destroy
stock = Stock.find(params[:id])
@user_stock = UserStock.where(user_id: current_user.id, stock_id: stock.id).first
@user_stock.destroy!
flash[:notice] = 'Stock successfully removed'
redirect_to my_portfolio_path
end
为什么即使我为它设置了id 也没有检测到我的路线?
【问题讨论】:
标签: ruby-on-rails rspec