Ruby on rails rspec测试强参数并建立模型

Ruby on rails rspec测试强参数并建立模型,ruby-on-rails,rspec,Ruby On Rails,Rspec,我正在尝试编写一个模型测试,如下所示: 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&

我正在尝试编写一个模型测试,如下所示:

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
class Five9List < ActiveRecord::Base

  attr_accessible :name, :size

end
目前,这项技术已经成功并运行良好。这是因为我的模型使用attr_accessible,如下所示:

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
class Five9List < ActiveRecord::Base

  attr_accessible :name, :size

end
并且删除attr_accessible不起作用

编辑 这是我从
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
故障:
1) Five9List#name返回正确的名称
失败/错误:@five9_list.name.should eql“test_list”
应为:“测试列表”
得:零
(使用eql进行比较?)
#./spec/models/five9_list_spec.rb:16:in‘block(3层)in’
2) Five9List#size返回正确的大小
失败/错误:@five9_list.size.eql 100
预期:100
得:零
(使用eql进行比较?)
#./spec/models/five9_list_spec.rb:22:in‘block(3层)’in’
以0.03303秒完成
4个示例,2个故障,1个待定
失败的示例:
rspec./spec/models/five9_list_spec.rb:15#Five9List#name返回正确的名称
rspec./spec/models/five9_list_spec.rb:21#Five9List#size返回正确的大小
随机化种子20608

您的规范没有问题。我只能猜测您没有运行Rails 4,或者您安装了ProtectedAttributes gem。

您需要错误的参数(:name)正确检查您的参数并将其传递到require()中……我想应该是require(:five9_list)…如果您使用rails默认REST路由,我修复了上述问题,而我使用的是默认REST路由。不过还是不走运。您的测试不涉及任何控制器代码,因此控制器中的任何内容都不重要,路由也不重要。我的数据库中有相应的列。还是没有骰子,钉死了。我安装了ProtectedAttributes gem,我认为这只是增加了功能,而不是妨碍了强大的参数功能。谢谢你的提示!