[rspec-users] Trouble with Capybara and RSpec on a new model spec

18 views
Skip to first unread message

Mostafa Hussein

unread,
May 1, 2013, 8:07:50 PM5/1/13
to rspec...@rubyforge.org
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: "mai...@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>

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Vighnesh Rege

unread,
May 1, 2013, 8:26:28 PM5/1/13
to rspec-users

On Wed, May 1, 2013 at 5:07 PM, Mostafa Hussein <li...@ruby-forum.com> wrote:

     before { visit mailers_path }

This should probably be
   before {visit new_mailer_path}

mailers_path takes you to /mailers -> MailerController#index method

Patrick J. Collins

unread,
May 1, 2013, 9:07:27 PM5/1/13
to rspec...@rubyforge.org
> it "should send a mail" do
> expect { click_button submit}.to change(Mailer, :count).by(1)
> end

In my opinion, capybara request specs are more for expressing the expectation of
what a user should see...

A user has no idea that clicking a button is going to increase some
database table's record count... So, I think a more useful test would
be on the controller level, which would do something like:

describe MailersController do
describe "#create" do
it "creates a mail record" do
expect { post :create, valid_mailer_params }.to change { Mailer.count }.by(1)
response.should redirect_to root_path
end

it "requires a valid attributes" do
expect { post :create, invalid_mailer_params }.to_not change { Mailer.count }
response.should render_template('mailers/new')
end
end
end

and then your request spec might end up just being something more simple
like:

describe "sending some mail" do
it "takes the user back to the home page" do
visit mailers_path
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: "mai...@example.com"
fill_in "Description", with: "something to say"
click_button "Send my Mail"
expect(page.current_path).to eq(root_path)
end
end

Patrick J. Collins
http://collinatorstudios.com

venkat damarasing

unread,
Jun 18, 2013, 7:55:49 AM6/18/13
to rspec...@rubyforge.org
Hi All,
Iam new to ruby application having little knowledge,and currently am
working for a testing application which is designed on
Ruby,capybara,rspec and am having an issue i.e when am click on a
button a javascript popup is displaying and sometime modalpopup also
displaying but am unable to click the alert and even i have tried
different ways with the help of google but none of them worked, for
example the script which i have used for clicking the alert is below

1.alert=page.driver.browser.switch_to.alert
alert.send(‘Stay on Page’)

2. page.evaluate_script(‘window.confirm = function() { return true; }’)
page.click(“Stay on Page”)

Coming to my frame work we have four files
1. Testcase.rb file---here the logic which test case required which
looks like following for details find in the attachment
require 'spec_helper'
feature 'Brochure Sample' do
#Config changes
app = Application.new
#Specify Object Maps
scenario 'Apply To All Levels Sample Messages' do
begin
app.utilities.setup_directory('Apply To All Levels Sample Messages')
#Specify Test data
#Launch the web site, and open Application
rescue Exception => e
app.utilities.capture_screen_shot('script failed','fail')
raise e
end
end
end

2.class file
The methods required for the test case
3. Spec Helpers
This contains some configuration i.e about drivers it looks like the
below for details find the attachment

require ‘capybara’
require ‘capybara/rspec’
require ‘capybara/dsl’
require ‘selenium-webdriver’
require ‘yaml’
#require ‘capybara-screenshot/rspec’

Capybara.default_driver = :selenium
Capybara.run_server = false
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
#Capybara configuration changes from the default that are needed.
Capybara.configure do |config|
config.match = :prefer_exact
config.exact_options = true
config.ignore_hidden_elements = true
config.visible_text_only = true
end

end

require_relative ‘configuration’
require_relative ‘helpers/common/application’

#this recursively pulls all files under the ‘helpers’ folder.
#puts ‘Current Directory:-’ + File.dirname(__FILE__)
#puts [File.join(File.dirname(__FILE__), 'helpers', '*.rb')]
current_path = File.expand_path File.dirname(__FILE__)
#puts “CURRENT” + current_path
helpers_path = File.join current_path, “helpers”
#puts “HELPERS” + helpers_path
Dir["#{helpers_path}/**/*.rb"].each { |file|
require file
#puts file
}

4.common
This is about utility i.e. the common methods used in the project
5.configuration
This contains the required urls,credentials etc

so can anyone please help me for clicking the alerts,modalpopups what
are the changes that i need to do for my framework please guide for
solving this issue

Attachments:
http://www.ruby-forum.com/attachment/8517/spechelperbfile.JPG
http://www.ruby-forum.com/attachment/8518/Testcasefile_.rbfile_.JPG
http://www.ruby-forum.com/attachment/8519/commonfiles.JPG
http://www.ruby-forum.com/attachment/8521/class_file.JPG

Reply all
Reply to author
Forward
0 new messages