【问题标题】:Functional test unable to locate template功能测试找不到模板
【发布时间】:2014-04-29 01:00:19
【问题描述】:

我正在尝试编写一个测试来验证我的控制器的操作。手动测试时,代码按预期执行。但是,我的测试引发了错误,因为它无法找到我正在尝试呈现的模板。

来自我的控制器代码的片段:

response = {:success => true}

response[:html] = {}
response[:html][:list] = render_to_string :partial => "data_source", :locals => {:data_source => @data_source}

respond_to do |format|
  format.json {render :json => response}
end

我的测试:

before do
  @data_source = FactoryGirl.create :data_source, :facebook_fan_page, :account_id => @account.id, :user_id => @user.id
  post :delete, :format => :json, :id => @data_source.id
end

it "should disable the data source" do
  assert_equal false, @data_source.reload.enabled?
end

错误信息:

ActionView::MissingTemplate:缺少部分数据源/数据源, 捕获/数据源,应用程序/数据源,带有 {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :haml]}。搜索于:
*“/Users/me/code/my_app/app/views”*“/Users/me/code/my_app/vendor/bundle/ruby/1.9.1/gems/konacha-3.0.0/app/views” 从 /Users/me/code/my_app/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.17/lib/action_view/path_set.rb:58:in `查找'

app/view/data_sources目录下肯定有部分“_data_source.html.haml”。

为什么测试找不到部分?我怀疑是设置问题

提前致谢。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    当您进行 json 请求时,render_to_string 正在寻找 json 模板。试试这个:

    render_to_string :partial => "data_source", :formats => [:html], :locals => {:data_source => @data_source}
    

    【讨论】:

    • 谢谢!我现在看到了这个问题。挖掘我的代码,当我发出请求时在本地运行它的格式为 / ,因此它部分正确地呈现了部分,但我的测试正在请求:json.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 2011-07-05
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 2012-05-23
    相关资源
    最近更新 更多