Rails查看未满足Rspec测试期望

时间:2014-02-18 02:03:09

标签: ruby-on-rails ruby rspec capybara

我认为Rspec视图测试正在搞乱,因为测试所寻找的链接不是实际视图的一部分,而是应用程序布局标题的一部分。

例如,我有以下Rspec代码:

  describe 'signups/new.html.erb' do

  before :each do
    render template: "signups/new", layout: "layouts/application"
  end

  it 'displays link to request pages not signup page' do
    rendered.should have_link("Make a new request", href: new_request_path)
    rendered.should_not have_link("Add me to the mailing list", href: new_signup_path)
  end

我得到的错误是找到了“将我添加到邮件列表”链接。但我向你保证,在本地服务器中情况并非如此。

视图中发生的事情如下(再次来自布局/应用程序文件,而不是来自注册/新):

<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <a class="navbar-brand" href="<%= root_path %>">Home</a>
      <a class="navbar-brand" href="<%= new_request_path %>">Make a new request</a>
      <% if current_page?(new_signup_path) == false %>
        <a class="navbar-brand" href="<%= new_signup_path %>">Add me to the mailing list</a>
      <% end %>
    </div>
  </div>
</nav>

基本上它是一个If / Then声明当你在注册页面时,不要显示将用户发送到注册页面的链接。然而,Rspec表明这个链接存在。有什么想法吗?

我正在考虑测试不是插入if / then语句,但不确定如何解决这个问题。

2 个答案:

答案 0 :(得分:0)

puts page.body行之前放置rendered.should have_link("Make a new request", href: new_request_path)我认为您的规范甚至不在页面上。

喜欢这样

it 'displays link to request pages not signup page' do
  puts page.body
  rendered.should have_link("Make a new request", href: new_request_path)
  rendered.should_not have_link("Add me to the mailing list", href: new_signup_path)
end

编辑:

重新阅读这个问题,我之前的建议并不是那么有用,尽管puts page.body对于帮助测试是一件好事。我想也许是因为你正在渲染注册/新的,但这不是路径,所以它不是“当前页面”。也许试试redirect_to。

尝试使用visit new_signups_path或其他路径(请查看我rake routes的{​​{1}}。您可能(我认为,但这取决于您的其他测试)摆脱之前:每个

答案 1 :(得分:0)

在测试开始时,您需要使用Capybara导航到注册页面。

例如,如果您的注册路由是/ signup,则可以调用

visit '/signup'

在每个街区之前。