0

I am attempting to write a model test, like so:

require 'spec_helper'

describe Five9List do
  before :each do
    @five9_list = Five9List.new(name: 'test_list', size: '100')
  end

  describe "#new" do
    it "takes two parameters and returns a Five9List object" do
      @five9_list.should be_an_instance_of Five9List
    end
  end

  describe "#name" do
    it "returns the correct name" do
      @five9_list.name.should eql "test_list"
    end
  end
  describe "#size" do
    it "returns the correct size" do
      @five9_list.size.should eql 100
    end
  end
end

Currently, this succeeds and works fine. That's because my model is using attr_accessible, like so:

class Five9List < ActiveRecord::Base

  attr_accessible :name, :size

end

If I want to get rid of attr_accessible and follow the rails 4 convention of using strong_params, how would I write that to where my rspec test would still succeed?

Adding this in my controller:

private
  def five9_list_params
    params.require(:five9_list).permit(:name, :size)
  end

And removing attr_accessible does not work.

EDIT

Here is the error I receive from rspec .:

Failures:

  1) Five9List#name returns the correct name
     Failure/Error: @five9_list.name.should eql "test_list"

       expected: "test_list"
            got: nil

       (compared using eql?)
     # ./spec/models/five9_list_spec.rb:16:in `block (3 levels) in <top (required)>'

  2) Five9List#size returns the correct size
     Failure/Error: @five9_list.size.should eql 100

       expected: 100
            got: nil

       (compared using eql?)
     # ./spec/models/five9_list_spec.rb:22:in `block (3 levels) in <top (required)>'

Finished in 0.03303 seconds
4 examples, 2 failures, 1 pending

Failed examples:

rspec ./spec/models/five9_list_spec.rb:15 # Five9List#name returns the correct name
rspec ./spec/models/five9_list_spec.rb:21 # Five9List#size returns the correct size

Randomized with seed 20608
3
  • u are requiring wrong param (:name) check your params properly and pass it inside require()...here i think it should be require(:five9_list).....if u are using rails default REST routes Commented Feb 13, 2014 at 18:18
  • I fixed the above, and I am using default REST routes. Still no luck though. Commented Feb 13, 2014 at 21:17
  • Your test doesn't involve any controller code, so nothing in your controller matters and your routes don't matter either. Commented Feb 14, 2014 at 0:14

1 Answer 1

2

There's nothing wrong with your spec. I can only guess that you're not running Rails 4 or you've installed the ProtectedAttributes gem.

Sign up to request clarification or add additional context in comments.

2 Comments

I do have the appropriate columns in my DB. Still no dice.
Nailed it. I had the ProtectedAttributes gem installed, I was thinking that only added functionality - not hindered the strong_params functionality. Thanks for the tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.