我正在测试我的rails应用程序,到目前为止,这个测试失败了所有其他人通过。
测试如下:
test "should create broadcast" do
assert_difference('Broadcast.count') do
post :create, broadcast: { title: "title", content: "content", user_id: @broadcast.user_id}
end
assert_redirected_to broadcast_path(assigns(:broadcast))
end
并且控制器有以下代码段:
feed_result = []
if params.has_key?(:feeds)
feed_result = params[:feeds]
else
feed_result = ["None"]
end
if @broadcast.save
# Only after saving do we try and do the real broadcast. Could have been
# done using an observer, but I wanted this to be more explicit
results = BroadcastService.broadcast(@broadcast, feed_result)
if results.length > 0
# Something went wrong when trying to broadcast to one or more of the
# feeds.
@broadcast.errors[:base] << ("#{I18n.t('broadcasts.unable-message')}: #{results.inspect}")
flash[:error] = I18n.t('broadcasts.saved-but-message')
else
flash[:notice] = I18n.t('broadcasts.saved-message')
no_errors = true
end
if no_errors
format.html { redirect_to(broadcasts_url(page: @current_page)) }
format.json { render json: @broadcast, status: :created, location: @broadcast }
else
format.html { render :new }
format.xml {
# Either say it partly worked but send back the errors or else send
# back complete failure indicator (couldn't even save)
if results
render json: @broadcast.errors, status: :created, location: @broadcast
else
render json: @broadcast.errors, status: :unprocessable_entity
end
}
end
end
end
我遗漏了不必要的信息。必须注意的是,如果我没有运行测试,并且我实际上正在运行实时服务器,则此create方法按预期运行,并且不会引发任何错误。
我尝试了以下内容:
post :create, broadcast: { title: "title", content: "content", user_id: @broadcast.user_id, format: :html}
post :create, broadcast: { title: "title", content: "content", user_id: @broadcast.user_id, format: :json}
post :create, broadcast: { title: "title", content: "content", user_id: @broadcast.user_id, format: 'html'}
post :create, broadcast: { title: "title", content: "content", user_id: @broadcast.user_id, format: 'json'}
错误仍然存在。
如果有人有任何想法,我很乐意听到他们的声音!
谢谢, 克里斯。