【发布时间】:2014-03-26 13:24:54
【问题描述】:
我正在为用户制作一个简单的应用程序来召开会议。
我的种子.rb 文件
marshall = User.create!(name: 'Marshall', email: 'lanners.marshall@yahoo.com',密码:'1234567',密码确认:'1234567') test_post1 = marshall.meetings.create!(title: "testpost!", time: '09:00:PM', date: "2014-01-22")
当我运行我的种子文件时,它会加载一切正常,并且当我加载页面时它在视图中工作。我可以看到与会议关联的用户名。但是,当我使用 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 文件#模型
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_
关于“登录” 结束
夹具 会议.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') %>
有人明白为什么它在视图中工作而不是在测试中工作吗?
【问题讨论】:
-
您是否尝试过仅单独显示会议或用户对象?
-
你的意思是在视图中?如果我显示会议,它就可以工作,而且我还有一个 users/index.html.erb 可以显示所有用户并且工作正常。
-
我的意思是在测试中。如果您只是显示会议 ID 或其他内容,您仍然会收到错误消息。我在测试关联对象时遇到了问题,我的第一步是查看它们是否在测试中独立工作。
-
我想我有,但以防万一我会通过更新我的原始帖子一秒钟来向您展示我的完整测试文件和助手
标签: ruby-on-rails capybara minitest