我正在编写验收测试以检查是否单击了一个按钮,它会被重定向到另一个页面(购物车页面),但我在尝试执行此操作时遇到错误。
我的代码是: -
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'
行时,控件不会进入控制器中的create方法。因此,我无法检查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
资源: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
答案 0 :(得分:3)
似乎您的表单不正确。
= simple_form_for :gift_card, url: gift_cards_path, method: :post do |f|
%form
%fieldset
.form-inputs
%form
元素simple_form_for @gift_card do |f|
答案 1 :(得分:1)
根据您分享的控制器代码,看来您的before_filter
或resource.errors.messages.any?
失败并且您呈现new
视图而不是重定向到carts_path
。没有任何其他代码,没有什么可说的。