在rpec中检查数组的长度(实例变量)

时间:2016-07-25 22:14:54

标签: 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 :(得分:0)

describe CountriesController do 
  describe 'index' do
    it 'has a country for each capital' do
      #create 3 countries here something like
      3.times do 
       Country.create(capitol: :capitol)
      end

      get :index

      expect(assigns(countries.length)).to eq (assigns(capitals.length))

    end
  end
end

答案 1 :(得分:0)

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

希望这有帮助。