集成测试正在返回一个“缺失的模板”。错误。 microposts_interface_test.rb
上的/members
错误。这可能是因为/ members视图使用了MicropostsController,但我不确定如何修复测试。
ERROR["test_micropost_interface", MicropostsInterfaceTest, 2015-06-19 06:40:32 +0800]
test_micropost_interface#MicropostsInterfaceTest (1434667232.75s)
ActionView::MissingTemplate: ActionView::MissingTemplate: Missing template /members with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
* "/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates"
* "/home/me/Development/my_app/app/views"
* "/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/app/views"
* "/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/phrasing-4.0.0rc9/app/views"
app/controllers/microposts_controller.rb:14:in `create'
test/integration/microposts_interface_test.rb:16:in `block (2 levels) in <class:MicropostsInterfaceTest>'
test/integration/microposts_interface_test.rb:15:in `block in <class:MicropostsInterfaceTest>'
app/controllers/microposts_controller.rb:14:in `create'
test/integration/microposts_interface_test.rb:16:in `block (2 levels) in <class:MicropostsInterfaceTest>'
test/integration/microposts_interface_test.rb:15:in `block in <class:MicropostsInterfaceTest>'
MicropostsInterfaceTest:
require 'test_helper'
class MicropostsInterfaceTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "micropost interface" do
log_in_as(@user)
get members_path
assert_select 'div.pagination'
assert_select 'input[type=file]'
# Invalid submission
assert_no_difference 'Micropost.count' do
post microposts_path, micropost: { content: "" }
end
assert_select 'div#error_explanation'
# Valid submission
content = "This micropost really ties the room together"
picture = fixture_file_upload('test/fixtures/rails.png', 'image/png')
assert_difference 'Micropost.count', 1 do
post microposts_path, micropost: { content: content, picture: picture }
end
assert assigns(:micropost).picture?
assert_redirected_to root_url
follow_redirect!
assert_match content, response.body
# Delete a post.
assert_select 'a', text: 'delete'
first_micropost = @user.microposts.paginate(page: 1).first
assert_difference 'Micropost.count', -1 do
delete micropost_path(first_micropost)
end
# Visit a different user.
get user_path(users(:archer))
assert_select 'a', text: 'delete', count: 0
end
test "micropost sidebar count" do
log_in_as(@user)
get members_path
assert_match "#{@user.microposts.count} microposts", response.body
# User with zero microposts
other_user = users(:mallory)
log_in_as(other_user)
get members_path
assert_match "0 microposts", response.body
# Create a micropost.
other_user.microposts.create!(content: "A micropost")
get members_path
assert_match "1 micropost", response.body
end
end
更新
# controller/members_controller.rb
class MembersController < ApplicationController
before_filter :logged_in_user
def index
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
我确认app/views/members/index.html.erb
处有一个特定的索引视图。
更新2:
# controller/microposts_controller.rb
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to members_path
else
@feed_items = []
render members_path
end
end
def destroy
@micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || members_path
end
private
def micropost_params
params.require(:micropost).permit(:content, :picture)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to members_path if @micropost.nil?
end
end
答案 0 :(得分:2)
在MicropostController的创建操作中,使用render "members/index"
而不是render members_path
。请注意,渲染不会加载成员/索引中所需的变量,它只会加载模板,所以像@micropost = []
一样添加@feed_items = []
,这样如果你从视图中调用这些变量就不会得到一个无法找到变量@microposts的错误。或者你可以简单地redirect_to members_path
。
答案 1 :(得分:0)
对我有用:
render template: 'template', formats: [:html]