简体   繁体   English

rspec水豚选择器滑轨4

[英]rspec capybara selector rails 4

I am trying to write an rspec test that will; 我正在尝试编写一个将要的rspec测试;

  • visit the store page 访问商店页面
  • select a value in the Countries select box 在国家选择框中选择一个值
  • and test that the value exists (initially in the Countries box but I would also be testing for cities which is dependent on Country in the second test) 并测试该值是否存在(最初在“国家/地区”框中,但我还将在第二项测试中测试依赖于国家/地区的城市)

HTML page HTML页面

<!DOCTYPE html>
<html>
  <head>
    <title>Ruby on Rails Tutorial Sample App</title>
    <link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/custom.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/password_resets.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/people.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/products.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/sessions.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/static_pages.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/store.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/users.css?body=1" media="all" rel="stylesheet" />
    <script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/bootstrap.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/password_resets.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/people.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/products.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/sessions.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/static_pages.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/store.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/users.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/application.js?body=1"></script>
    <meta content="authenticity_token" name="csrf-param" />
<meta content="NcsV3Ve7QqLTjeLNz5MLuCxzvG8urc63NiDk7RZTGtM=" name="csrf-token" />
    <!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

  </head>
  <body>
    <header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <a href="/" id="logo">sample app</a>
      <nav>
        <ul class="nav btn-group navbar-nav navbar-right list-inline">
                  <li><a href="/">Home</a></li>
          <li><a href="/help">Help</a></li>
            <li><a href="/signin">Sign in</a></li>
        </ul>
        </div>
        </ul>
      </nav>
    </div>
  </div>
</header>


      <div class="container">




<h2>some text</h2>
<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <div class="row">
      <div class="col-md-6 col-md-offset-2">
        <form accept-charset="UTF-8" action="/store/show" method="get"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /></div>

          <label for="catalogue_country">Country</label>
          <select id="countries_select" name="catalogue[country_id]"><option selected="selected" value="1">France</option>
<option value="2">Italy</option>
<option value="3">United Kingdom</option></select>

        <div class="pull-right">
          <input class="btn brn-large btn-primary" name="commit" type="submit" value="Add to Cart" />
</form>        </div>
      </div>
    </div>
  </div>
</div>


        <footer class="footer">
  <small>
    <a href="http://railstutorial.org/">Rails Tutorial</a>
    by Michael Hartl
  </small>
  <nav>
    <ul>
      <li><a href="/about">About</a></li>
      <li><a href="/contact">Contact</a></li>
      <li><a href="http://news.railstutorial.org/">News</a></li>
    </ul>
  </nav>
</footer>


      <pre class="debug_dump">--- !ruby/hash:ActionController::Parameters
controller: store
action: index
</pre>

    </div>
  </body>

</html>

My test 我的测试

require 'spec_helper'

describe "index" do

  subject { page }

  before do

    visit store_path

    select "United Kingdom", :from => "catalogue[country_id]"

  end

  it { should have_select('country_id', :selected => 'United Kingdom') }

end

The failures 失败

Failures: 失败:

1) index Failure/Error: visit store_path NoMethodError: undefined method `id' for nil:NilClass 1)索引失败/错误:访问store_path NoMethodError:nil:NilClass的未定义方法'id'

The rails console [app.store_path] verifies that store_path is '/store' Rails控制台[app.store_path]验证store_path为'/ store'

The select boxes are dynamically populated according to the directions in this blog posting 根据此博客文章中的说明动态填充选择框

Any help is appreciated, thanks in advance 任何帮助表示赞赏,在此先感谢

There are the routes 有路线

  match '/store', to: 'store#index', as: :store, via: 'get'

  get 'store/show'

  match 'store/update_cities', to: 'store#update_cities',  as: :update_cities, via: 'get'

  match 'store/update_currencies', to: 'store#update_currencies',  as: :update_currencies, via: 'get'

Its one index page (store_path) which has a countries select box. 它的一个索引页面(store_path)带有一个国家/地区选择框。

This is the store controller 这是商店控制器

  def index

    @countries = Country.all

    @cities = City.where("country_id = ?", Country.first.id)

    @currencies = Currency.where("country_id = ?", Country.first.id)

    @locations = Location.where("city_id = ?", Location.first.id)

    @products = Product.where("Location_id = ?", Product.first.id)

    @plugs = Plug.where("Location_id = ?", Plug.first.id)

  end

  def show
  end

  def update_cities

    @cities = City.where("country_id = ?", params[:country_id])

    respond_to do |format|

      format.js

    end
  end

Are you trying to visit a specific store, or listing all the stores ? 您是要访问特定商店还是列出所有商店?

in your case you are trying to visit a specific store which you need to pass a parameter for it in this case the store id 在您的情况下,您尝试访问的是特定商店,您需要为此传递一个参数,在这种情况下,商店ID

To know the exact path write in the console: 要知道确切的路径,请在控制台中编写:

rake routes

you will find each path to its url, if you want to list all the stores you need to write: 如果要列出需要编写的所有商店,则会找到其URL的每个路径:

stores_path

instead of: 代替:

store_path

store_path is /store/:id store_path是/ store /:id

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

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