简体   繁体   中英

rspec capybara selector rails 4

I am trying to write an rspec test that will;

  • 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

<!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

The rails console [app.store_path] verifies that store_path is '/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.

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

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:

stores_path

instead of:

store_path

store_path is /store/:id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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