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

Patrick J. Collins patrick at collinatorstudios.com
Thu May 2 01:07:27 UTC 2013

>        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

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

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

  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: "mailer at example.com"
      fill_in "Description",  with: "something to say"
      click_button "Send my Mail"
      expect(page.current_path).to eq(root_path)

Patrick J. Collins

More information about the rspec-users mailing list