vendredi 27 mai 2016

PG::UndefindColumn: ERROR on only at action test

I'm getting this test error when I run my test by "bundle exec rake test"

ERROR["test_unsuccessful_edit", UsersEditTest... ActionView::Template::Error: ActionView::Template::Error: PG::UndefinedColumn: ERROR: column groups_users.group_id does not exist

It works fine on development. My test script is below.

# test "unsuccessful edit" do
#   log_in_as(@user)
#   get edit_user_path(@user)
#   assert_template 'users/edit'
#   patch user_path(@user), user: { name: "",
#   email: "foo@invalid",
#   password: "foo",
#   password_confirmation: "bar",
#  }
#   assert_template 'users/edit'
# end

As you can see, I spiked this test since it apparently nothing wrong in the development environment.

The error message is saying "group_id" column does not exist in table "groups_users". But it's actually exist since I've run migration and checked database/behavior by hands.

Is there any way to make this test script work?

In my models, as you may be guessing, I have below relationships method.

• User.rb

  has_and_belongs_to_many :groups, join_table: :groups_users, dependent: :destroy

• Group.rb

  has_and_belongs_to_many :user, join_table: :groups_users

And my migration file looks like this:

class GroupsUsers < ActiveRecord::Migration
  def change
    create_table :groups_users, :id => false do |t|
        t.integer :group_id
        t.integer :user_id
    end
  end
end

There is no validation for these groups values to be present. Please let me know if you have any idea or questions.

Thanks,

Aucun commentaire:

Enregistrer un commentaire