Equivalent of should be_true in minitest
Newer versions of minitest allow testing predicates in assert_operator
. The expectation for that in the spec DSL is must_be
. So your code can look like:
event.must_be :live?
Rails controller test with Rspec
To test controller and views together you write feature specs and request specs .
Request specs are lower level specs where you send HTTP requests to your application and write expectations (aka assertions in TDD lingo) about the response. They are a wrapper around ActionDispatch::IntegrationTest
. Request specs should be considered the replacement for controller specs, the use of which are discouraged by by the RSpec and Rails teams.
# spec/requests/products_spec.rb
require 'rails_helper'
RSpec.describe "Products", type: :request do
describe "GET /products" do
let!(:products) { FactoryBot.create_list(:product, 4) }
it "contains the product names" do
get "/products"
expect(response).to include products.first.name
expect(response).to include products.last.name
end
end
end
Feature specs are higher level specs that focus on the user story. They often serve as acceptance tests. They use a browser simulator named Capybara which emulates a user clicking his way through the application. Capybara can also run headless browsers (headless chrome, firefox, phantom js, webkit etc) and "real" browsers through selenium. The minitest equivalent is ActionDispatch::SystemTestCase
but RSpec features do not wrap it (it took minitest/testunit years to catch up here).
# Gemfile
gem 'capybara'
# spec/features/products_spec.rb
require 'rails_helper'
RSpec.feature "Products" do
let!(:products) { FactoryBot.create_list(:product, 4) }
scenario "when a user views a product" do
visit '/'
click_link 'Products'
click_link products.first.name
expect(page).to have_content products.first.name
expect(page).to have_content products.first.description
end
end
This specs tests the products#index and products#show action as well as the root page and the associated views.
Both types of specs have their strengths and weaknesses. Feature tests are good for testing large swaths of the application but are heavy. Request specs are faster and its easier to replicate a specific request that causes a bug/issue but you're basically just matching HTML with regular expressions which is highly limited.
rspec assert_equal no method error
Try object.should eq('foo')
or expect(object).to eq('foo')
instead. As a side note, object.should == 'foo'
will give you a Ruby warning when running with the -w
flag.
Also, this is an assumption answer since you didn't give us any actual code.
Rails minitest devise error
Only include the Devise helpers in your controller tests. Your test_helper.rb
file should look like this:
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
#require "minitest/rails/capybara"
# Uncomment for awesome colorful output
#require "minitest/pride"
require 'minitest/autorun'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionController::TestCase
include Devise::TestHelpers
end
RSpec 3.1 undefined method `feature' for main:Object
It looks like your forgot to require capybara at your spec/rails_helper.rb
require 'capybara/rspec'
Also you can try to remove this line:
config.disable_monkey_patching!
Or check if capybara adds feature method to Rspec namespace:
RSpec.feature "My feature" do
...
end
RubyMine can't find RSpec specs
Looks like RubyMine is running RSpec in the spec directory. By default, RSpec looks in a subdirectory, named spec, of the directory it's run in. RubyMine seems to be misconfigured. Either something is wrong with that particular run/debug configuration or the RubyMine project thinks that the spec directory (rather than the directory above it) is the project root.
You can confirm that RSpec itself is OK on the command line by cd
ing to the root of your project and running bin/rspec
.
Related Topics
Git Bash Chcp Windows7 Encoding Issue
Activerecord::Associationtypemismatch in Controller#Create on Dropdown Select for a Rails Self Join
Ruby Is Already Using the Class Name of My Model
How to Get a Listing of Only Files Using Dir.Glob
Are There More Elegant Ways to Prevent Negative Numbers in Ruby
Detecting Regional Settings (List Separator) from Web
Case-Insensitive Find_Or_Create_By_Whatever
How to Run Perl and Ruby Scripts as Tasks in Ant
How to Change Ruby to Version 1.9.3 (Again) with Rvm
Activerecord_Postgis_Adapter: Undefined Method 'Point' for Nil:Nilclass
Ruby on Rails: Params Is Nil. Undefined Method '[]' for Nil:Nilclass
How to Mock Super in Ruby Using Rspec
How to Use Headless Chrome with Capybara and Selenium