Reputation: 9075
I am writing an acceptance test to check on clicking a button it gets redirected to another page(carts page), but I am getting an error while trying to do the same.
my code is:-
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
acceptance spec :-
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
error:-
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"
[Edit]
On execution of click_button 'create gift card'
line in the rspec, the control does not enter the create method in the controller. Because of which I am unable to check 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
Upvotes: 1
Views: 140
Reputation: 2410
Seems your form is incorrect.
= simple_form_for :gift_card, url: gift_cards_path, method: :post do |f|
%form
%fieldset
.form-inputs
%form
elementsimple_form_for @gift_card do |f|
Upvotes: 3
Reputation: 29409
Based on the controller code you shared, it appears that either it's failing your before_filter
or resource.errors.messages.any?
was truthy and you rendered the new
view rather than redirecting to carts_path
. Without any other code, there's not much else to tell.
Upvotes: 1