name", model_path(:sort_by => 'na-6ren">
gpt4 book ai didi

ruby-on-rails - RSpec 查看测试 : How to modify params?

转载 作者:行者123 更新时间:2023-12-03 23:59:59 26 4
gpt4 key购买 nike

我正在尝试使用 RSpec 测试我的观点。给我带来麻烦的特定 View 会根据 url 参数改变其外观:
link_to "sort>name", model_path(:sort_by => 'name')结果是 http://mydomain/model?sort_by=name
然后我的 View 使用这个参数:

<% if params[:sort_by] == 'name' %>
<div>Sorted by Name</div>
<% end %>

RSpec 看起来像这样:
it "should tell the user the attribute for sorting order" do
#Problem: assign params[:sort_for] = 'name'
render "/groups/index.html.erb"
response.should have_tag("div", "Sorted by Name")
end

我想在 RSpec 中测试我的 View (没有 Controller ),但我无法将此参数输入我的 params多变的。我试过 assign在所有不同的口味:
  • assign[:params] = {:sort_by => 'name'}
  • assign[:params][:sort_by] = 'name'
  • ...

  • 到目前为止没有成功。每个想法都值得赞赏。

    最佳答案

    如果它是 Controller 测试,那么它将是

    controller.stub!(:params).and_return {}

    如果它是一个辅助测试,那么它将是:
    helper.stub!(:params).and_return {}

    它是一个 View 测试,它将是:
    view.stub!(:params).and_return {}

    如果您收到如下警告。
    Deprecation Warnings:

    Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /home/akbarbin/Documents/Office/projects/portfolio/spec/views/admin/waste_places/new.html.erb_spec.rb:7:in `block (2 levels) in <top (required)>'.


    If you need more of the backtrace for any of these deprecations to
    identify where to make the necessary changes, you can configure
    `config.raise_errors_for_deprecations!`, and it will turn the
    deprecation warnings into errors, giving you the full backtrace.

    1 deprecation warning total

    Finished in 4.86 seconds (files took 4.72 seconds to load)

    你可以把它改成
    allow(view).to receive(:params).and_return({sort_by: 'name'})

    关于ruby-on-rails - RSpec 查看测试 : How to modify params?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1802742/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com