Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在rspec中,我为什么需要';[]和#x27;在expect()中设置为eq([])?_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 在rspec中,我为什么需要';[]和#x27;在expect()中设置为eq([])?

Ruby on rails 在rspec中,我为什么需要';[]和#x27;在expect()中设置为eq([])?,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,Rails应用程序,编写规范: RSpec.describe AdvertisementsController, :type => :controller do let(:my_ad) { Advertisement.create!(title: 'title', copy: 'copy text', price: 10)} describe 'GET #index' do ... ... it 'renders my_ad' do get :index,

Rails应用程序,编写规范:

RSpec.describe AdvertisementsController, :type => :controller do
  let(:my_ad) { Advertisement.create!(title: 'title', copy: 'copy text', price: 10)}

  describe 'GET #index' do
...
...

    it 'renders my_ad' do
      get :index, {id: my_ad.id}
      expect(assigns[:advertisements]).to eq(my_ad)
    end
  end
...
...
end
我写了上面的内容,给出了下面的错误

1) 广告控制器获取索引呈现我的广告 失败/错误:expect(分配[:广告])给eq(my_ad)


值得注意的是,我可以看到对象具有不同的标识id,因此我认为这是部分原因,
[…]
忽略了不匹配或其他原因,但我想了解它


-#
[]
是数组的Ruby语法。因此,您的测试期望分配一个广告数组(或者在本例中,行为类似于数组的东西,例如
ActiveRecord::Relation
),其中只包含一个元素
my_ad

您的代码听起来也很奇怪,因为您为索引操作提供了一个ID,只希望返回该记录。索引操作用于列出记录组-显示操作用于显示单个记录的详细信息

   expected: #<Advertisement id: 1, title: "title", copy: "copy text", price: 10, created_at: "2016-02-26 02:39:20", updated_at: "2016-02-26 02:39:20">
        got: #<ActiveRecord::Relation [#<Advertisement id: 1, title: "title", copy: "copy text", price: 10, created_at: "2016-02-26 02:39:20", updated_at: "2016-02-26 02:39:20">]>

   (compared using ==)

   Diff:
   @@ -1,8 +1,8 @@
   -#<Advertisement:0x007ff6995e19f0
   - id: 1,
   - title: "title",
   - copy: "copy text",
   - price: 10,
   - created_at: Fri, 26 Feb 2016 02:39:20 UTC +00:00,
   - updated_at: Fri, 26 Feb 2016 02:39:20 UTC +00:00>
   +[#<Advertisement:0x007ff6994f9560
   +  id: 1,
   +  title: "title",
   +  copy: "copy text",
   +  price: 10,
   +  created_at: Fri, 26 Feb 2016 02:39:20 UTC +00:00,
   +  updated_at: Fri, 26 Feb 2016 02:39:20 UTC +00:00>]
RSpec.describe AdvertisementsController, :type => :controller do
  let(:my_ad) { Advertisement.create!(title: 'title', copy: 'copy text', price: 10)}

  describe 'GET #index' do
...
...

    it 'renders my_ad' do
      get :index, {id: my_ad.id}
      expect(assigns[:advertisements]).to eq([my_ad])
    end                                      ^     ^
  end                                        |     |
...
...
end