Neil
Neil

Reputation: 569

Rails 4.2: Unable to write Rspec test for validations

Want to run a test on my blog application, to validate the title field and displays an error message when the user does not enter a title and clicks submit. The goes back to the index page. When i run the test using Rspec/capybara i get .

Failures:

  1) Validate the title articles field displays an error when the article no title
     Failure/Error: visit "/posts/new"
     NoMethodError:
       undefined method `visit' for #<RSpec::ExampleGroups::ValidateTheTitleArticlesField:0x007fa2cbe73e00>
     # ./spec/features/validates_title_spec.rb:5:in `block (2 levels) in <top (required)>'

validates_title_spec.rb

require 'spec_helper'

describe "Validate the title articles field" do 
it "displays an error when the article no title" do 
   visit "/posts/new"
   expect(page).to have_content("New Posts")

   fill_in "Title", with: ""
   click_button "Create Post"

   expect(page).to have_content("Title can't be blank")
   visit "/posts"
 end 
end

rake routes

Prefix Verb   URI Pattern               Controller#Action
     root GET    /                         welcome#index
    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new
edit_post GET    /posts/:id/edit(.:format) posts#edit
     post GET    /posts/:id(.:format)      posts#show
          PATCH  /posts/:id(.:format)      posts#update
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy

post.rb

class Post < ActiveRecord::Base
  validates :title, presence: true

end

_post.html.erb

<%= form_for @post do |f| %>
  <% if @post.errors.any? %>
    <h2><%= pluralize(@post.errors.count, "error") %> Prevented this post from saving: </h2>
    <ul>
      <% @post.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
    <% end %>

    <%= f.label :title %><br>
    <%= f.text_field :title %>

    <br>
    <br>

    <%= f.label :content, "Write your article here" %><br>
    <%= f.text_area :content %>

    <br>
    <br>

    <%= f.submit %>

  <% end %>

new.html.erb

<h1> New Posts </h1>

<%= render 'form' %>

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!
end

Upvotes: 0

Views: 147

Answers (2)

Marcin Urbanski
Marcin Urbanski

Reputation: 2493

Use this:

require 'rails_helper'

instead of this:

require 'spec_helper'

as the first line of validates_title_spec.rb file.

Upvotes: 1

hamitron
hamitron

Reputation: 219

You need to require Capybara in your spec helper:

require 'capybara/rails'

Upvotes: 0

Related Questions