(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 => "[email protected]" }
  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

Ouch embarrassing. :smiley:

Thanks!

On Apr 23, 8:58pm, Anders_P [email protected] wrote:

if @newsletter_receiver.save
require ‘spec_helper’
lambda do
post :create, :newsletter_reveiver => @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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs