【发布时间】:2014-12-06 23:19:12
【问题描述】:
我正在尝试修复以下 rails 功能测试:
test "should create broadcast" do
login_as(:one_details)
assert_difference('Broadcast.count') do
post :create, feeds: [:twitter, {alumni_email: 'test@email.com'}], broadcast: {id: 2, content: @broadcast.content, user: @user_details.user_id}
end
assert_redirected_to "#{broadcast_path}?page=1"
end
这是错误:
1) Error:
BroadcastsControllerTest#test_should_create_broadcast:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"broadcasts"} missing required keys: [:id]
test/controllers/broadcasts_controller_test.rb:35:in `block in <class:BroadcastsControllerTest>'
我在路由文件中有这个:
resources :broadcasts, except: [:edit, :update]
在 broadcasts_controller 中,show 方法如下所示:
def show
if is_admin?
respond_to do |format|
format.js { render partial: 'show_local',
locals: {broadcast: @broadcast, current_page: @current_page},
layout: false }
format.html # show.html.erb
format.json # show.json.builder
end
else
indicate_illegal_request I18n.t('not allowed to view this')
end
end
广播控制器创建方法:
定义创建
@broadcast = Broadcast.new(broadcast_params)
# Wire up broadcast with the current user (an administrator)
# Will be an admin user (see before_filter)
# Note the current_user is a user_detail object so we need
# to navigate to its user object
@broadcast.user = current_user.user
# Doing the next line forces a save automatically. I want to defer this
# until the "if" statement
#current_user.user.broadcasts << @broadcast
no_errors = false
respond_to do |format|
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, params[:feeds])
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
end
【问题讨论】:
标签: ruby-on-rails testing ruby-on-rails-4 functional-testing