`rspec` - 比较两个阵列'相互之间的长度

时间:2016-07-26 01:21:05

标签: ruby-on-rails rspec

index方法中有2个数组,我想测试数组的相等性。长度,但它不起作用(手动测试后阵列的长度相等)

def index
    @countries = get_countries()
    @capitals = get_capitals()
end

Rspec文件:

describe CountriesController do 
  describe 'index' do
    it 'countries.length == capitals.length' do
      expect(assigns(countries.length)).to eq (assigns(capitals.length))
    end
  end
end

2 个答案:

答案 0 :(得分:1)

看起来你没有向该行动提出请求......那就是get :index电话在哪里?

答案 1 :(得分:0)

应该是这样的:

describe CountriesController do 
  describe 'index' do
    it 'countries.length == capitals.length' do
      get :index
      expect(assigns(:countries).length).to eq assigns(:capitals).length
    end
  end
end