0
votes

I am new to Ruby on Rails, and I am coming across an UnknownAttributeError through my rspec tests. Where and how do you debug this issue? Thanks in advance.

I've looked through these questions, but my problem is NOT -

I am creating a login page with name, password, and password confirmation, along with a submit button by using form_for.

<h1>Welcome!</h1>
<h3>Please log in, stranger.</h3>
<%= form_for @user do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :password %>
  <%= f.text_field :password %>

  <%= f.label :password_confirmation %>
  <%= f.text_field :password_confirmation %>

  <%= f.submit %>
<% end %>

I also have a Sessions controller with actions that allows a session to be created when a user logs in:

class SessionsController < ApplicationController
  def new
  end

  def create
    @user = User.find_by(name: params[:user][:name])
  end
end

Here is my users controller, just in case, so that I'm not leaving anything out

class UsersController < ApplicationController
  def new
  end

  def create
    user = User.new(user_params).save
  end

  private

  def user_params
    params.require(:user).permit(:name, :password, :password_confirmation)
  end
end

"name" is an attribute in my users table. Here is my schema below:

ActiveRecord::Schema.define(version: 20170416184846) do

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "password_digest"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
  end
end

And I only have a ActionController class method before_action which shouldn't affect the unknown attribute error.

class User < ActiveRecord::Base
  has_secure_password
end

I also created a record in rails c, and I successfully saved one.

2.3.0 :002 > User.create(name: "Dave", password: "dave", password_confirmation: "dave")

(0.1ms) begin transaction SQL (0.5ms) INSERT INTO "users" ("name", "password_digest", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Dave"], ["password_digest", "$2a$10$bq9G.od4ecoTRWg0dlwREudjdDX5QFa7L.c1U2Jd6qoKmCnGNG61O"], ["created_at", "2017-04-16 20:41:04.061507"], ["updated_at", "2017-04-16 20:41:04.061507"]] (0.8ms) commit transaction => #

And finally, here are my routes:

Rails.application.routes.draw do
  get '/login', to: 'sessions#new'
  post '/login', to: 'sessions#create'
  delete '/logout', to: 'sessions#destroy'
end

Here is the rspec file for SessionsController:

require 'rails_helper'

RSpec.describe SessionsController, type: :controller do

  before do
    User.destroy_all
  end

  let(:connie) {User.create(name: 'Connie', password: 'M4heswaran')}

  describe 'post create' do
    it 'logs you in with the correct password' do
      post :create, user: {name: connie.name, password: connie.password}
      expect(session[:user_id]).to eq(connie.id)
    end

    it 'rejects invalid passwords' do
      post :create, user: {name: connie.name, password: connie.password + 'x'}
      expect(session[:user_id]).to be_nil
    end

    it 'rejects empty passwords' do
      post :create, user: {name: connie.name, password: ''}
      expect(session[:user_id]).to be_nil
    end
  end
end
2
Where do you see that message @Iceman?user7559874
Could you add the entire sessions_controller_spec?Anton
Sure, I just updated the question with spec file for SessionsController @Antonuser7559874
Do puts connie at the top of your test and check out the log. Is there a valid object? Why don't you do user: connie instead of the hash?Anton
I got "connie", and my tests ran the same afterwards. I actually ran bin/rake db:migrate RAILS_ENV=development, and that helped clear the unknown attribute error! Thanks @Iceman @Antonuser7559874

2 Answers

5
votes

You have to set up your test database also with this command from the terminal

rails db:test:prepare

One other way would be

RAILS_ENV=test rails db:migrate 

Can read more about this all in the excellent guides

0
votes

If you have just started and no data in your database start from fresh

Drop the database:

rake db:drop

Create all database:

rake db:create

prepare your test db:

rake db:test:prepare

migrate your db:

rake db:migrate

then migrate your db for test env:

rake db:migrate RAILS_ENV=test