简体   繁体   English

Rspec,Rails - 如何测试使用params hash的helper方法?

[英]Rspec, Rails - how to test helper method that use params hash?

I want to implement a tagging system similar to stackoverflow, there is a box with a tags at top right corner, and also I have links to delete tag from params hash. 我想实现一个类似于stackoverflow的标记系统,右上角有一个带标签的框,我还有从params hash删除标签的链接。 my method works correctly in browser. 我的方法在浏览器中正常工作。 But I can't find a way to test it. 但我找不到测试它的方法。

def tags_list_with_destroy_links
    if params[:tags]
      li = ""
      p = params[:tags].split("+") # '/tagged/sea+ship+sun' => ['sea', 'ship', 'sun']
      p.map do |t|
        remove_link = if p.count  >= 3
                        c = p.reject {|item| item == t }
                        a = c.join("+")
                        {:tags => a}
                      elsif p.count == 2
                        c = p.reject {|item| item == t }
                        {tags: c[0]}
                      else
                        questions_url
                      end

        li << content_tag(:li) do
          link_to(t, questions_tags_path(t), class: 'tag') +
              link_to( '', remove_link , class: 'icon-small icons-cross')
        end
      end

      ul = content_tag(:ul, li.html_safe)
      ul << tag(:hr)
    end
  end

I've tried: 我试过了:

it 'return list with selected tags' do
      #Rails.application.routes.url_helpers.stub(:questions_tags).and_return('/questions/tagged/sea+ship+sun')
      #helper.request.stub(:path).and_return('/questions/tagged/sea+ship+sun')
      helper.stub(:url_for, {controller:'questions', action: 'index', tags:'sea+ship+sun'}  ).and_return('/questions/tagged/sea+ship+sun')
      helper.params[:tags] = 'sea+ship+sun'
      helper.tags_list_with_destroy_links.should == 'list_with_tags'
    end

but it return: 但它返回:

<a class=\"tag\" href=\"/questions/tagged/sea+ship+sun\">sea</a><a class=\"icon-small icons-cross\" href=\"/questions/tagged/sea+ship+sun\"></a></li>

and shoud return remove link as 并且shoud返回删除链接为

href="/questions/tagged/ship+sun" without sea 没有海的href =“/ questions / tagged / ship + sun”

I would appreciate any advice 我很感激任何建议

The params field is going to come back parsed into the correct ruby data structures (hash, array, string, etc). params字段将返回解析为正确的ruby数据结构(哈希,数组,字符串等)。 There's no need to manually split items such as + , if there is a nested param it will return as part of the params object: 没有必要手动拆分+等项,如果有嵌套参数,它将作为params对象的一部分返回:

{tags: ["sea", "ship", "sun"]}

To access your data, or create an assumption about your param data existing in the test, you're going to want to create a stub . 要访问您的数据,或者创建关于测试中存在的参数数据的假设,您将需要创建存根 You're almost there, try something more along the lines of: 你几乎就在那里,尝试更多的东西:

helper.stub!(:params).and_return({tags: ["sea", "ship", "sun"]})

Once you have the params stubbed correctly you can check the output of your helper method to ensure it's validity (this is called the expectation ): 一旦你有正确的params存根,你可以检查助手方法的输出,以确保它的有效性(这称为期望 ):

expect(helper.tags_list_with_destroy_links).to eq("some_url")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM