0

I'm making a simple app for users to make meetings.

my seeds.rb file

marshall = User.create!(name: 'Marshall', email: 'lanners.marshall@yahoo.com', password: '1234567', password_confirmation: '1234567') test_post1 = marshall.meetings.create!(title: "testpost!", time: '09:00:PM', date: "2014-01-22")

when I run my seeds file it loads up everything fine and it works in the view when I load the page. I can see the users name associated with the meeting. However, when I run my tests with minitest capybara I get the follow error.

test_0005_can destroy meeting                             0:00:01.131 ERROR
        undefined method `name' for nil:NilClass
        Exception `ActionView::Template::Error' at:
        app/views/meetings/index.html.erb:44:in `block in _app_views_meetings_index_html_erb___3264872258283250738_70171632466460'

and the error

test_0002_can update meeting                              0:00:00.716 ERROR
        undefined method `name' for nil:NilClass
        Exception `ActionView::Template::Error' at:
        app/views/meetings/show.html.erb:4:in `_app_views_meetings_show_html_erb___1801038855426537038_70299721197600'

I was wondering what I did wrong because I ran it works fine in the view but not in the test. Here are some files of mine to help you guys see what I'm doing wrong.

meetings/index.html.erb

<% @meetings.each do |meeting| %>
  <tbody>
    <tr>
           #below is line 44
      <td><%= meeting.user.name %></td>
      <td><%= meeting.title %></td>
      <td><%= meeting.time.strftime("%l:%M %p") %></td>
      <td><%= meeting.date %></td>
      <td><%= link_to "show page", meeting %></td>
      <td><%= link_to "edit meeting", edit_meeting_path(meeting) %></td>
      <td><%= link_to "destroy", meeting, method: :delete, data: {confirm: 'are you sure'} %></td>
    </tr>
  </tbody>

schema.rb file

  create_table "meetings", force: true do |t|
    t.string   "title"
    t.time     "time",       limit: 255
    t.datetime "created_at"
    t.datetime "updated_at"
    t.date     "date"
    t.integer  "user_id"
  end

  create_table "users", force: true do |t|
    t.string   "email"
    t.string   "name"
    t.string   "password_digest"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

meeting.rb file #model

class Meeting < ActiveRecord::Base
  has_many :complaints
  belongs_to :user
  validates :title, :time, :date, presence: true
end

user.rb file # model

class User < ActiveRecord::Base
  has_secure_password

  has_many :meetings
  has_many :complaints
end

and here is the test that is not working features/meeting_system_test.rb

require "test_helper"

feature "as an office manager,
         I want a working meeting system
         so that people will show up to meetings" do

  scenario "can create new meeting" do
    log_in
    visit new_meeting_path
    meeting_one
    page.must_have_content "meeting created"
  end

  scenario "can update meeting" do
    log_in
    visit edit_meeting_path(Meeting.first)
    click_on "Update Meeting"
    page.must_have_content "meeting updated"
  end

  scenario "create meeting validations work" do
    log_in
    visit new_meeting_path
    click_on "Create Meeting"
    page.must_have_content "3 errors"
  end

  scenario "update meeting validations work" do
    log_in
    visit edit_meeting_path(Meeting.first)
    empty_meeting
    page.must_have_content "3 errors"
  end

  scenario "can destroy meeting" do
    log_in
    visit meetings_path
    within ".table-striped" do
      click_on("destroy", :match => :first)
    end
    page.must_have_content "meeting deleted"
  end

end

user_system_test.rb

require "test_helper"

feature "as an office manager,
         I want a working user system
         so that workers can create accounts and log in" do

  scenario "create user do" do
    visit new_user_path
    create_user
    page.must_have_content "user created"
  end

  scenario "can user " do
    visit edit_user_path(User.first)
    fill_in "Password", with: "password"
    fill_in "Password confirmation", with: "password"
    click_on "Submit"
    page.must_have_content "user updated"
  end

  scenario "create user validations work" do
    visit new_user_path
    click_on "Submit"
    page.must_have_content "6 errors"
  end

  scenario "update user validations work" do
    visit edit_user_path(User.first)
    empty_user
    page.must_have_content "6 errors"
  end

  scenario "can destroy user" do
    visit users_path
    within ".table-striped" do
      click_on("destroy", :match => :first)
    end
    page.must_have_content "user deleted"
  end
end

test_helper.rb

def empty_meeting
  fill_in "Title", with: " "
  fill_in "Time", with: " "
  fill_in "Date", with: " "
  click_on "Update Meeting"
end

def meeting_one
  fill_in "Title", with: meetings(:one).title
  fill_in "Time", with: meetings(:one).time
  fill_in "Date", with: meetings(:one).date
  click_on "Create Meeting"
end

def create_user
  fill_in 'Name', with: "the dude"
  fill_in 'Email', with: "thedude@cool"
  fill_in "Password", with: 'password'
  fill_in "Password confirmation", with: 'password'
  click_on "Submit"
end

def empty_user
  fill_in "Name", with: " "
  fill_in "Email", with: " "
  click_on "Submit"
end

def log_in
  visit new_session_path
  fill_in "Email", with: users(:user).email
  fill_in "Password", with: "password"
  click_

on "Sign in" end

fixtures meetings.yml

one:
  title: MyString
  time: 03:00:PM
  date: 2014-01-14

two:
  title: MyString
  time: 05:00:AM
  date: 2014-01-11

users.yml

user:
  email: user@examle.com
  name: user
  password_digest: <%= BCrypt::Password.create('password') %>

admin:
  email: user2@example2.com
  name: user2
  password_digest: <%= BCrypt::Password.create('password') %>

anyone understand why its working in the view but not the test?

  • Have you tried just displaying the meeting or user object independently? – NM Pennypacker Mar 26 '14 at 14:11
  • you mean in the view? I it works if I show the meeting and I also have a users/index.html.erb that shows all the users and that works fine. –  Mar 26 '14 at 14:24
  • I mean in the test. If you just display the meeting id or something do you still get the error. I've run into problems testing associated objects and my first step is to see if they work independently in test. – NM Pennypacker Mar 26 '14 at 14:30
  • I think I have but just in case I will show you my full test file and helper by updating my orignal post one sec –  Mar 26 '14 at 14:40

1 Answers1

0

You are getting this error because your meeting records don't have a user record associated with them. You can add this easily. In your meeting test data add the user records you want to associate the meeting with. So, in your test/fixtures/meetings.yml file have the following:

one:
  title: MyString
  time: 03:00:PM
  date: 2014-01-14
  user: user

two:
  title: MyString
  time: 05:00:AM
  date: 2014-01-11
  user: admin
blowmage
  • 8,854
  • 2
  • 35
  • 40