【问题标题】:unable to find link in rspec无法在 rspec 中找到链接
【发布时间】:2013-12-28 23:59:45
【问题描述】:

这是我在 spec/controllers/movies_controller_spec.rb 中的 rspec 代码的一部分:

it 'should call a controller method to receive the click on
"Find With Same Director", and grab the id of the subject' do     
  Movie.stub('find_similar_movies')
  controller.should_receive('similar_movies').
          with(hash_including :id => '3')
  visit movie_path('3')
  click_link('Find Movies With Same Director')
end

我总是得到错误:

Failure/Error: click_link('Find Movies With Same Director')
     Capybara::ElementNotFound:
       Unable to find link "Find Movies With Same Director"

这是我在 app/views/movies/show.html.haml 中的部分观点:

%h2 Details about #{@movie.title}

%ul#details
  %li
    Rating:
    = @movie.rating
  %li
    Director:
    = @movie.director
  %li
    Released on:
    = @movie.release_date.strftime("%B %d, %Y")

%h3 Description:

%p#description= @movie.description

= link_to 'Edit', edit_movie_path(@movie)
= button_to 'Delete', movie_path(@movie), :method => :delete, :confirm => 'Are you sure?' 
= link_to 'Back to movie list', movies_path
= link_to 'Find Movies With Same Director', similar_movies_path(@movie)

我也用相同的步骤运行黄瓜:访问相同的页面,然后单击链接,它通过了。但是在 rspec 中,我没有通过测试。

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    默认情况下,视图不会在 RSpec 控制器测试中呈现,因此您的 Capybara find_link 方法正在寻找空虚而无法成功。

    您可以通过在describe 块的顶部包含对render_views 的调用来“打开”示例组的视图呈现,如https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/render-views 中所述

    但是,控制器旨在使用其他机制进行测试,如 https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs 中所述

    另见How do I test whether my controller rendered the right layout on Rails 3?

    【讨论】:

    • 谢谢,就是这样!我检查了 page.html,它什么也没返回。但我无法弄清楚几个小时的原因。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多