我是rails(和英语:)的初学者,我正在尝试使用函数测试,但我一开始就出错了
1)Error:
test_should_get_new(MicropostControllerTest)
NoMethodError: undefined method 'microposts' for nil:NilClass我的micropost_controller_test.rb
require 'test_helper'
class MicropostControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
end我的micropost_controller.rb
class MicropostController < ApplicationController
def new
@post = Micropost.new
@posts = current_user.microposts.all
end
def create
@post = current_user.microposts.create(:content => params[:content])
logger.debug "New post: #{@post.attributes.inspect}"
logger.debug "Post should be valid: #{@post.valid?}"
if @post
redirect_to micropost_new_path
else
end
end
end我试着在microposts.yml里放点东西,但是没有用。那么,在哪里我可以找到微生物学方法进行功能测试,我如何修复呢??请帮帮我?
p/s:我的应用程序仍然在本地主机上工作。
发布于 2014-10-28 11:12:25
如果您正在使用设计进行用户身份验证,那么您需要在MicropostController中进行身份验证和设置current_user,例如,拥有一个before_action,如下所示:
class MicropostController < ApplicationController
before_action :authenticate_user!
def new
@post = Micropost.new
@posts = current_user.microposts.all
end
# rest of the code
end在您的测试中,如果您还没有在test_helper中这样做,则需要按以下方式导入设计测试帮助程序
class MicropostControllerTest < ActionController::TestCase
include Devise::TestHelpers
end然后,您可以使用sign_in方法在测试中使用固定装置在用户中签名。搜索一些关于这方面的教程,或者查看这里的响应,以获得一些线索:Functional testing with Rails and Devise. What to put in my fixtures?
https://stackoverflow.com/questions/26605986
复制相似问题