Mike
Mike

Reputation: 169

Checking the length of the arrays(instance variables) in rpec

Having 2 arrays in index method, I want to test the equality of the arrays' lengths but it's not working (the lengths of the arrays are equal after testing manually)

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

Rspec file:

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

Upvotes: 0

Views: 1600

Answers (2)

Mayur Vaidya
Mayur Vaidya

Reputation: 11

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

Hope this helps.

Upvotes: 1

ruby_newbie
ruby_newbie

Reputation: 3285

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

Upvotes: 0

Related Questions