Trouble with Capybara and RSpec on a new model spec

I have searched a lot for a solution, but I could not find it! I am trying to make a rails application after watching Michael Hartl's tutorial and when i fill data manually and submit it nothing happens and nothing records in the Mailers table and I am facing the following error:

Failures:

  1) Mailer pages mail us with valid information should send a mail
     Failure/Error: expect { click_button submit}.to change(Mailer, :count).by(1)
       count should have been changed by 1, but was changed by 0
     # ./spec/requests/mailer_pages_spec.rb:31:in `block (4 levels) in <top (required)>'

Finished in 0.89134 seconds
2 examples, 1 failure

Failed examples:

rspec ./spec/requests/mailer_pages_spec.rb:30 # Mailer pages mail us with valid information should send a mail

Randomized with seed 17352

The model file is:

class Mailer < ActiveRecord::Base
  attr_accessible :company_name, :contact_name, :address, :telephone, :email, :description
  before_save { |mailer| mailer.email = mailer.email.downcase }
  validates :company_name, length: { maximum: 50 }
  validates :contact_name, presence: true, length: { maximum: 40 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
end

Controller:

class MailersController < ApplicationController

  def new
    @mailer = Mailer.new
  end

  def create
    @mailer = Mailer.new(params[:mailer])
    if @mailer.save
      redirect_to root_path
    else
      render 'new'
    end
  end
end

Integration test:

require 'spec_helper'

describe "Mailer pages" do

 subject { page }


 describe "mail us" do

     let(:submit) { "Send my Mail"}
     before { visit mailers_path }


     describe "with invalid information" do
       it "should not send a mail" do
         expect { click_button submit }.not_to change(Mailer, :count)
         end
     end

     describe "with valid information" do
       before do
         fill_in "Company name", with: "Mailer Company"
         fill_in "Contact name", with: "Mailer Contact"
         fill_in "Address",      with: "Mailer Address"
         fill_in "Telephone",    with: "123-456-789"
         fill_in "Email",        with: "mailer@example.com"
         fill_in "Description",  with: "something to say"
       end

       it "should send a mail" do
         expect { click_button submit}.to change(Mailer, :count).by(1)
       end  
     end
   end
 end

And the form:

<% provide(:title , 'Mail Us') %>
<h1>Mail Us</h1>
<div class="row">
    <div class="span6 offset3 hero-unit">

<%= form_for(@mailer) do |f| %>
    <%= f.label :company_name %>
    <%= f.text_field :company_name %>

    <%= f.label :contact_name %>
    <%= f.text_field :contact_name %>

    <%= f.label :address %>
    <%= f.text_field :address %>

    <%= f.label :telephone %>
    <%= f.text_field :telephone %>

    <%= f.label :email %>
    <%= f.text_field :email %>

    <%= f.label :description %>
    <%= f.text_field :description %>

    <%= f.submit "Send my Mail", class: "btn btn-large btn-primary"%>
<% end %>
</div>
</div>

After <%= debug(params) if Rails.env.development? %> added to application layout , I saw that the action is new not create , is that what causing the problem ?? waiting for help

Answers


Your create action specifies that if the object is saved you should be redirected to the root_path, otherwise it should render the form again (render 'new').

Therefore if the form is rendering again then it means validations are failing and your Mailer object hasn't been saved. Add the following to your form to see the errors:

<%= form_for(@mailer) do |f| %>
  <% @mailer.errors.full_messages.each do |msg| %>
    <p><%= msg %></p>
  <% end %>
  ...
<% end %>

Need Your Help

Huffman encoding slowing me down

c# list encoding huffman-coding

I think this part of my code is slowing me down. How can I change it so it is faster. I have to read war and piece. it works with smaller samples but will not read war in piece without taking less ...

Including decimal equivalent of a char in a character array

c++ string character-encoding ascii

Is there any way i can create a character array with decimal numbers instead of actual characters?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.