用于JavaScript响应的RSpec控制器测试

时间:2012-10-04 22:58:58

标签: ruby-on-rails ajax rspec

我有一个只响应AJAX调用的控制器操作:

def archive
  @model = MyEngine::MyModel.find(params[:id])
  if @model.update_attributes(params[:model])
    @success = true
  else
    @success = false
  end
  respond_to do |format|
    format.js
  end
end

我想用以下内容测试:

it "should respond with javascript" do
  xhr :post, :archive, {:id => @model.to_param, :use_route => :my_engine}, valid_session
  response.should render_template('asi/events/archive', :format => 'js')
end

it "should not respond with html" do
  xhr :post, :archive, {:id => @event1.to_param, :use_route => :asi}, valid_session
  response.should_not render_template('asi/events/archive', :format => 'html')
end

但似乎断言主要涉及渲染'存档'模板,并不关心它的扩展。如果有更好的方法来规范这个问题,我有什么想法吗?

感谢您的期待!

1 个答案:

答案 0 :(得分:-3)

尝试

response.should_not render_template 'asi/events/archive.html'

另外,你可以稍微重构你的模型

@success = @model.update_attributes(params[:model])

@success = @model.update_attributes(params[:model]) === true