2016-07-25 56 views
0

index方法中有2個數組,我想測試數組長度是否相等,但它不工作(手動測試後數組長度相等)在rpec中檢查數組的長度(實例變量)

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 
+0

[\ rspec \' - 將兩個數組的長度相互比較]的可能重複(http://stackoverflow.com/questions/38579733/rspec-comparing-two-arrays-lengths-against-each-other ) –

回答

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 
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 

希望這有助於。