测试capybara minitest嵌套资源时出错

时间:2014-03-26 13:24:54

标签: ruby-on-rails capybara minitest

我正在为用户制作一个简单的应用程序来开会。

my seeds.rb file

marshall = User.create!(姓名:' Marshall',电子邮件: ' lanners.marshall@yahoo.com' ;,密码:' 1234567',password_confirmation:' 1234567') test_post1 = marshall.meetings.create!(标题:" testpost!",时间:' 09:00:PM',日期:" 2014-01-22&#34 ;)

当我运行我的种子文件时,它加载一切正常,当我加载页面时它在视图中工作。我可以看到与会议相关联的用户名。但是,当我用minitest capybara运行我的测试时,我得到了跟随错误。

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'

和错误

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'

我想知道我做错了什么,因为我跑了它在视图中工作正常但不在测试中。以下是我的一些文件,可以帮助你们了解我做错了什么。

会议/ 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文件

  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文件#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

这是不起作用的测试 特征/ 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

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&#34;登录&#34; 端

夹具 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') %>

任何人都明白为什么它在视图中工作而不是测试?

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为您的会议记录没有与之关联的用户记录。您可以轻松添加。在会议测试数据中添加要与会议关联的用户记录。因此,在您的test/fixtures/meetings.yml文件中包含以下内容:

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