(Beginner) Learning Rspec (help!)

Hello!

I'm trying to make a simple test. I have a "coming soon" page with a
field where users can submit their emails. When the users clicks
"submit" it should create a new record in the database. I Have two
controllers:

class PagesController < ApplicationController
  def home
    @newsletter_receiver = NewsletterReceiver.new
  end
end

And:

class NewsletterReceiversController < ApplicationController
  def create
    @newsletter_receiver =
NewsletterReceiver.new(params[:newsletter_receiver])
    if @newsletter_receiver.save
      flash[:notice] = "A message"
      redirect_to root_path
    else
      render 'pages/home'
    end
  end
end

My NewsletterReceiversController specs looks like this:

require 'spec_helper'

describe NewsletterReceiversController do

  describe "Success" do

      before(:each) do
        @attr = { :email => "example@example.com" }
      end

      it "should create a receiver" do
        lambda do
          post :create, :newsletter_reveiver => @attr
        end.should change(NewsletterReceiver, :count).by(1)
      end
    end
  end
end

This gives me the following error:

Failure/Error: lambda do
  count should have been changed by 1, but was changed by 0

Anyone got any tips on how to solve this?

Thanks
// Anders

Hello!

And:

class NewsletterReceiversController < ApplicationController
def create
@newsletter_receiver =
NewsletterReceiver.new(params[:newsletter_receiver])
if @newsletter_receiver.save
flash[:notice] = "A message"
redirect_to root_path
else
render 'pages/home'
end
end
end

My NewsletterReceiversController specs looks like this:

require 'spec_helper'

describe NewsletterReceiversController do

describe "Success" do

  before\(:each\) do
    @attr = \{ :email =&gt; &quot;exam\.\.\.@example\.com&quot; \}
  end

  it &quot;should create a receiver&quot; do
    lambda do
      post :create, :newsletter\_reveiver =&gt; @attr
    end\.should change\(NewsletterReceiver, :count\)\.by\(1\)
  end

Step through your code and you'll see that you've got a typo here.

Fred

Ouch embarrassing. :smiley:

Thanks!