# Trouble with Capybara and RSpec on a new model spec

**URL:** <https://www.ruby-forum.com/t/trouble-with-capybara-and-rspec-on-a-new-model-spec/228659>\
**Category:** RSpec\
**Created:** [May 2, 2013, 2:07am UTC](https://www.ruby-forum.com/t/trouble-with-capybara-and-rspec-on-a-new-model-spec/228659 "2013-05-02T02:07:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![code-vortex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@code-vortex](https://www.ruby-forum.com/u/code-vortex)\
**Post date:** [May 2, 2013, 2:07am UTC](https://www.ruby-forum.com/t/trouble-with-capybara-and-rspec-on-a-new-model-spec/228659/1 "2013-05-02T02:07:50Z")

</div>

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 H.'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: "removed_email_address@domain.invalid"
     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’) %\>

# Mail Us

\<%= 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 class="post-metadata">

**Author:** ![code-vortex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@code-vortex](https://www.ruby-forum.com/u/code-vortex)\
**Post date:** [June 18, 2013, 1:55pm UTC](https://www.ruby-forum.com/t/trouble-with-capybara-and-rspec-on-a-new-model-spec/228659/2 "2013-06-18T13:55:49Z")

</div>

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’)

1. 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 [RubyDoc.info: Class: RSpec::Core::Configuration – Documentation for rspec-core (3.13.6) – RubyDoc.info](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
