我正在編寫一個驗收測試來檢查點擊按鈕時,它會被重定向到另一個頁面(購物車頁面),但我在嘗試執行相同操作時出錯。問題在接受測試,同時重定向到另一頁
我的代碼是: -
gift_cards_controller.rb
class GiftCardsController < ApplicationController
before_filter :get_order_and_shipment, only: [:create]
def create
super do |format|
if resource.errors.messages.any?
format.html { render :new }
else
format.html { redirect_to carts_path }
end
end
end
end
驗收規範: -
gift_card_spec.rb
require 'spec_helper'
include Warden::Test::Helpers
Warden.test_mode!
describe 'Gift Card', type: :feature do
before(:all) do
Address.delete_all
end
it 'redirects to cart page' do
user = create(:user)
login_as(user, scope: :user)
address = create(:address, name: 'ABC',
street: 'PQR',
postal_code: '12345',
state_or_region: 'XYZ',
phone_number: '123-456-7890',
city: 'LMN',
user: user)
add_str = 'ABC, PQR, LMN, XYZ, 12345'
visit new_gift_card_path
page.should_not have_content('Please add address before creating a gift card')
page.should have_button('create gift card')
fill_in 'gift_card_name', with: 'Gift Card'
fill_in 'gift_card_value', with: 1000.99
fill_in 'gift_card_message', with: "This is A Gift Card"
option = first('#gift_card_address_list option').text
expect(option).to eq(add_str)
click_button 'create gift card'
expect(page.current_url).to eq(carts_url)
page.should have_content('ORDER SUMMARY')
end
end
錯誤: -
Gift Card
redirects to cart page (FAILED - 1)
Failures:
1) Gift Card redirects to cart page
Failure/Error: expect(page.current_url).to eq(carts_url)
expected: "http://www.example.com/cart"
got: "http://www.example.com/gift_cards/new?gift_card[code]=75C4FC&gift_card[name]=Gift+Card&gift_card[value]=1000.99&gift_card[message]=This+is+A+Gift+Card&commit=create+gift+card"
[編輯]
論RSpec的click_button 'create gift card'
線的執行期間,控制不進入在控制器中創建方法。因爲這些,我無法檢查resourse.errors.messages.any?
view/gift_cards/new.html.haml
#giftcard
.giftCard
%h1
Create A Gift Card
= simple_form_for :gift_card, url: gift_cards_path, method: :post do |f|
%form
%fieldset
.form-inputs
= f.input :code,input_html: {value: GiftCard.generate_code}, required: true
= f.input :name, label: 'Gift Card Name/Caption', required: false
= f.input :value, label: 'Amount'
= f.input :address_list, collection: current_user.addresses,
label_method: :get_address_string, include_blank: false
= f.input :message, as: :text, required: false
= f.button :submit, 'create gift card',
class: 'gift_card_submit', disable_with: "creating your gift card..."
routes.rb
resources :gift_cards
gift_cards_path GET /gift_cards(.:format) gift_cards#index
POST /gift_cards(.:format) gift_cards#create
new_gift_card_path GET /gift_cards/new(.:format) gift_cards#new
edit_gift_card_path GET /gift_cards/:id/edit(.:format) gift_cards#edit
gift_card_path GET /gift_cards/:id(.:format) gift_cards#show
PUT /gift_cards/:id(.:format) gift_cards#update
DELETE /gift_cards/:id(.:format) gift_cards#destroy
謝謝回答@Peter。您希望我添加哪部分代碼? –
你應該知道爲什麼驗證在這裏失敗(這裏有什麼錯誤信息'resource.errors.messages.any?')並且改變你的測試來構建一個有效的資源。 – gotva
@gotva我編輯了這個問題。如上所述,由於控件未輸入控制器的創建方法,因此我無法查看錯誤。你有其他建議嗎?請告訴我,謝謝 –