vendredi 24 avril 2015

Capybara Rspec integration test link_to POST gives back ActionView::Template::Error:

It' my first time working with capybara and Rspec together for integration testing. I was able to link around a couple of routes and fill in the required fields from the form. Everything up to "visit webinars_path" in my spec works ok.

The trouble I am encountering is this line "click_link 'Register Now' " link. It gives me the error:

Failure/Error: find_link('Register Now', :href => '/webinars/test-webinar/register').click ActionView::Template::Error: undefined method `permalink' for nil:NilClass

I am assuming this means that @webinar is nil, however when I debug this in the Registration controller (see below registrations_controller.rb), using puts @webinar.permalink It logs back 'test-webinar' which is the webinar I want to register for. So, I know that registrations#new is passing, but it can't get past that to registraions#create. Can anyone help me solve this error, wondering why @webinar is nil.

register_for_webinar_spec.rb

require 'rails_helper'
require 'spec_helper'
require  'capybara/rspec'

feature 'Register for Webinar', :type => :feature do 

def create_webinar(options={})
    visit "/admin"
    click_link "Create Webinar"
    options[:name] ||= "Test Webinar"
    options[:permalink] ||= "test-webinar"
    options[:broadcast_date] ||= "2017-02-25 00:00:00"
    options[:gotowebinar_key] ||= "111-111-111"

    fill_in "webinar_name", with: options[:name]
    fill_in "webinar_permalink", with: options[:permalink]
    fill_in "webinar_broadcast_date", with: options[:broadcast_date]
    fill_in "webinar_gotowebinar_key", with: options[:gotowebinar_key]

    click_button "Create"
 end

 def register_attendee(options={})
    find_link('Register Now', :href => '/webinars/test-webinar/register').click

    options[:first_name] ||= "John"
    options[:last_name] ||= "Doe"
    options[:company] ||= "JD CO"
    options[:email] ||= "John_Doe@gmail.com"

    fill_in "attendee_first_name", with: options[:first_name]
    fill_in "attendee_last_name", with: options[:last_name]
    fill_in "attendee_company", with: options[:company]
    fill_in "attendee_email", with: options[:email]
 end 

scenario 'registers attendee to webinar' do 
    create_webinar
    expect(page).to have_content("New Webinar is Created")

    visit webinars_path
    register_attendee

end 

end

registrations_controller.rb

require 'json'
require 'net/http'
class RegistrationsController < ApplicationController

def new
  get_webinar
  get_webinar_details

  @attendee = Attendee.new
  puts @webinar.permalink
end

def create
  get_webinar


  @attendee =  Attendee.new(attendee_params)
  @attendee.webinar = @webinar

  if @attendee.save
    redirect_to registration_path(@webinar.permalink)
  else
    flash[:error] = @attendee.errors

    get_webinar_details
    render 'new'
  end
end

def show
  get_webinar
  @webinars = Webinar.where.not(video_url: '').order('broadcast_date DESC')
end

private

def attendee_params
  params.require(:attendee).permit(:first_name, :last_name, :email, :company, :webinar_id, :ip_address, :address, :latitude, :longitude)
end
end

webinar index.html

<% if @broadcast_webinars.exists? %>
  <% @broadcast_webinars.each do |webinar| %>
  <%= link_to webinar_path(webinar.permalink) do %>
  <div class="webinar-container centered upcoming all" data-category="upcoming">
    <% end %>
    <div class="left-section"></div>
    <div class="middle-section">
      <h3><%= webinar.name%></h3>
      <span class="posted-date"><strong><%= webinar.broadcast_date.strftime("%A") %>, &nbsp;<%= webinar.broadcast_date.strftime("%d") %>&nbsp;<%= webinar.broadcast_date.strftime("%B") %> &nbsp;<%= webinar.broadcast_date.strftime("%Y") %></strong></span>
      <div class="info centered">
        <i class="fa fa-play-circle"></i>
      </div>
        <p><%= webinar.description %></p>
        <a href=<%= register_path(webinar.permalink) %>><button>Register Now </button></a>
    </div>
    <div class="right-section">
      <p><span><%= webinar.broadcast_date.strftime("%d") %></span><br><span><%= webinar.broadcast_date.strftime("%B") %></span>
    </div>
    <div class="bottom-section">
      <div class="bottom-1">
        <p> 
          <% if webinar.duration != ""%>
          <%= webinar.duration %> mins
          <% end %>
        </p>
      </div>
      <div class="bottom-2">
        <p class="author"><span><i class="fa fa-user"></i>AUTHORS:</span><%= webinar.author%> 
          <% if webinar.author2 != ""%>
          , <%= webinar.author2%></p>
          <% end %>
      </div>
      <div class="bottom-3">
        <p class="tags"><span><i class="fa fa-tags"></i></span><%= webinar.tag %></p>
      </div>
      </div>
    </div>
  <% end %>
<% end %>

Aucun commentaire:

Enregistrer un commentaire