3

Hello I have been looking for a way to test model relationship and stumble upon should gem

  • shoulda (3.5.0)
  • shoulda-context (1.2.1)
  • shoulda-matchers (2.8.0)

Unfortunatly I tried to test a simple example with rspec

describe Region do
  it  "should have a city" do 
    should belong_to(:city)
  end
end

and I always get a message of

Region should have a city
     Failure/Error: should belong_to(:city)
       Expected Region to have a belongs_to association called city (Region does not have a city_id foreign key.)
     # ./spec/models/region_spec.rb:5:in `block (2 levels) in <top (required)>'

I though that there was something wrong with my relationships but I have test to create a Region with a city tied to it on rails console successfully. I must be missing something!!

Edit Models and migration

class Region < ActiveRecord::Base
    belongs_to :city
end


class City < ActiveRecord::Base
    validates :name, :presence => true
    has_many :regions
end

And I created the city after the region so had to modify the migration file a bit:

class CreateCities < ActiveRecord::Migration
  def change
    create_table :cities do |t|
      t.string :name
      t.float :longitude
      t.float :latitude
      t.timestamps
    end

    add_reference :regions, :city, index: true, foreign_key: true

  end
end

schema.rb

  create_table "cities", force: true do |t|
    t.string   "name"
    t.float    "longitude"
    t.float    "latitude"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "regions", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "city_id"
  end

  add_index "regions", ["city_id"], name: "index_regions_on_city_id"
Necronet
  • 6,704
  • 9
  • 49
  • 89
  • 1
    You need to post your `Region` and `City` models and the relevant part of your `schema.rb` so that we can assess your data is set up correctly. – Kelsey Hannan Aug 19 '15 at 02:19
  • Again please post your schema.rb for Region and City, not your migration of City. Finally please provide what version of Rails and Shoulda you are using. – Kelsey Hannan Aug 19 '15 at 02:48

1 Answers1

1
Region does not have a city_id foreign key.

Your error message clearly points out the problem. As, Region belongs_to a City, it's expecting a city_id foreign_key in Region Model.

Add a city_id column in your Region model by a migration and then this test will work! I think, nothing is wrong here with the shoulda gem. It's just your current model setup.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • yes it does seem pretty clear but I still need to figure out why since I am able to link a Region to a city, checked schema.rb and database and I can see a `city_id integer` field, – Necronet Aug 19 '15 at 16:03
  • do you really need `add_reference :regions, :city, index: true, foreign_key: true` to add your foreign key? It could be an issue. There could be gem version compatibility issue. I could not find anything on that though. still trying. In the mean time, try removing that line, just keep `city_id` column in `regions` table and the proper association which you already have. Then try. Let me know! – K M Rakibul Islam Aug 19 '15 at 23:40
  • One thing just came into my mind. did you run: `RAILS_ENV=test rake db:migrate`? It could be your test database that does not have the `city_id` in `regions` table. Please test this and let me know! Try: `RAILS_ENV=test bundle exec rails c` then try to create a `Region` with a `city` tied to it on rails console (pointing to test database). Let me know about it. – K M Rakibul Islam Aug 20 '15 at 01:01