想要在我的博客应用程序上运行测试,验证标题字段并在用户未输入标题并单击提交时显示错误消息。回到索引页面。当我使用Rspec / capybara运行测试时,我得到了。
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
佣金路线
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
答案 0 :(得分:1)
使用此:
require 'spec_helper'
而不是:
validates_title_spec.rb
作为$excelObj->getActiveSheet()->getProtection()
$excelObj->getActiveSheet()->getProtection()->isProtectionEnabled()
$excelObj->getActiveSheet()->getProtection()->getPassword()
文件的第一行。
答案 1 :(得分:0)
您需要在规范助手中要求Capybara:
require 'capybara/rails'