I'm having a 'Undefined method let' error while trying to run a minitest spec.
Here's the spec:
require 'test_helper'
extend Minitest::Spec::DSL
class AccountTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
let(:entity) { Entity.create }
let(:user) { User.create(plan: 0, entity: entity) }
test 'valid account' do
account = Account.new(user_id: user, name: 'Нал', type: :cash, currency: 'RUB', category: :regular)
assert account.valid?
end
end
Here's the test_helper file:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/autorun'
require 'shoulda/context'
require 'shoulda/matchers'
require 'minitest/reporters'
Minitest::Reporters.use!
include FactoryGirl::Syntax::Methods
class ActiveSupport::TestCase
fixtures :all
end
Here's the part of a Gemfile:
group :development, :test do
gem 'binding_of_caller'
gem "minitest-rails"
gem 'shoulda'
gem 'minitest-reporters'
gem 'pry'
gem 'minitest-rails-capybara'
gem 'capybara'
gem 'factory_girl'
end
What's wrong with me? :)
You need to add the following require to your test helper:
require "minitest/rails"
If you add the require you don't need the extend Minitest::Spec::DSL in your test.
let is only defined within describe.
Try putting it inside describe
describe "let" do
let(:entity) { Entity.create }
let(:user) { User.create(plan: 0, entity: entity) }
Related
I'm trying to run this integration test. For some reason, it isn't accepting minitest syntax and I have no clue why.
test_helper.rb
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'minitest/reporters'
require "minitest/rails/capybara"
Minitest::Reporters.use!
class ActiveSupport::TestCase
fixtures :all
end
gemfile.rb
group :test do
gem "minitest-rails"
gem 'minitest-rails-capybara'
gem 'minitest-reporters'
gem 'guard'
gem 'guard-minitest'
gem "selenium-webdriver"
gem "chromedriver-helper"
gem "shoulda-matchers", require: false
end
integration_controller_test
require 'test_helper'
class MovieControllerTest < ActionDispatch::IntegrationTest
setup do
#movie = movies(:movie_1)
end
describe "integration testing" do
test 'get index' do
get movies_path
assert_response :success
end
end
end
I am using 'devise', '~> 3.5', '>= 3.5.6'.
My controller test work, but I can't get the integration test to run
test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/rails/capybara"
require "mocha/mini_test"
require "minitest/reporters"
require 'shoulda/context'
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # spec-like progress
include Devise::TestHelpers
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
include FactoryGirl::Syntax::Methods
fixtures :all
end
fixture:
default_user:
name: "User"
email: "example#example.org"
encrypted_password: "exampleexample"
example test file:
class PostsTests < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test 'authenticated users can see posts' do
sign_in users(:default_user)
end
end
Error:
NameError: uninitialized constant Devise::Test
I seemed to be working with an old version. I just updated to 4.1.1
gem 'devise', '~> 4.1', '>= 4.1.1'
I want Capybara to fill out a login form, click on 'Login', and redirect me to the root path like this: (example). This app is your classic blog app with Devise for authentication - I can't think of anything else that would affect the test.
I only get a 'failed assertion' test error so I checked the spelling of my button, experimented with Warden, and still couldn't figure it out. For a solution, I'd like to be able to exactly mimic how a user would navigate through a page.
Integration test:
require 'test_helper'
require 'database_cleaner'
class PostsTest < ActionDispatch::IntegrationTest
# for setup and tear down each time we run tests
self.use_transactional_fixtures = false
setup do
DatabaseCleaner.start
end
teardown do
DatabaseCleaner.clean
end
def setup
#post = posts(:valid_post)
#admin = users(:james)
#regular = users(:steve)
end
test 'display comments' do
assert #post.comments.first.body == 'hello'
end
test 'visit contact page' do
visit '/contact'
assert page.has_text?('jamesyoun710')
end
test 'login as admin' do
visit new_user_session_path
assert page.has_field?('Email', type: 'email')
fill_in('Email', with: 'jamesyoun710#gmail.com')
fill_in('Password', with: 'gooneen44')
click_on('Log in') # this doesn't redirect me into the page
# login_as(#admin) # I need to do this to execute the line above
visit root_path
assert page.has_link?('Logout')
assert page.has_link?('New Post')
end
end
test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
require 'minitest/rails'
require 'capybara/rails'
require 'minitest/rails/capybara'
Minitest::Reporters.use!
# for creating sessions in tests
include Warden::Test::Helpers
Warden.test_mode!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
end
class ActionController::TestCase
include Devise::TestHelpers
end
class ActionDispatch::IntegrationTest
include Capybara::DSL
end
gemfile:
group :development, :test do
gem 'database_cleaner'
gem 'hirb'
gem 'byebug'
gem 'annotate'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'better_errors', '~> 2.1.1'
gem 'binding_of_caller', '~> 0.7.2'
end
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'minitest-rails-capybara'
# gem 'mini_backtrace'
gem 'guard-minitest', '2.3.1'
end
Found the solution - I should have been using login info from my fixtures (test db) but instead I was using login info from my development db.
I have the followed
group :test, :development do
gem 'rspec-rails', '~> 3.0'
gem "guard-rspec"
gem 'capybara'
gem 'factory_girl_rails'
gem 'turn'
gem 'guard' # NOTE: this is necessary in newer versions
gem 'poltergeist'
gem 'phantomjs', :require => 'phantomjs/poltergeist'
gem 'selenium-webdriver'
gem 'capybara-screenshot'
gem 'capybara-webkit'
gem 'zeus'
gem "parallel_tests"
gem 'launchy'
gem 'email_spec'
gem 'action_mailer_cache_delivery'
gem 'protractor-rails'
gem 'database_cleaner'
and in my spec_helper.rb
require 'capybara/rspec'
require 'factory_girl_rails'
require 'support/request_helpers'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include RequestHelpers
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Finally I have such a spec helper in the spec/support/request_helper.rb
module RequestHelpers
def create_logged_in_user
user = create(:user)
login(user)
user
end
def login(user)
login_as user, scope: :user
end
end
and factories.rb
# /spec/factories/factories.rb
FactoryGirl.define do
factory :user do
first "John"
last 'Smith'
email 'test#example.com'
password 'johnsmith123'
end
end
Yes everytime I run rpsec it shows that
Factory not registered: user
And when I rune FactoryGirl.factories in the rails console, i can see 'user' is registered
=> #<FactoryGirl::Registry:0x007fa4e0e18160
#items=
{
:user=>
#<FactoryGirl::Factory:0x007fa4e2371200
#aliases=[],
#class_name=nil,
#compiled=false,
#definition=
#<FactoryGirl::Definition:0x007fa4e2370f58
#additional_traits=[],
#attributes=nil,
#base_traits=[],
#callbacks=[],
#compiled=false,
#constructor=nil,
#declarations=
#<FactoryGirl::DeclarationList:0x007fa4e2370ee0
#declarations=
[#<FactoryGirl::Declaration::Static:0x007fa4e2370b48 #ignored=false, #name=:first, #value="john">,
#<FactoryGirl::Declaration::Static:0x007fa4e2370a80 #ignored=false, #name=:last, #value="smith">,
#<FactoryGirl::Declaration::Static:0x007fa4e23709b8 #ignored=false, #name=:email, #value="test#example.com">,
I read through the Github setup docs for factory_girl_rails, rspec-rails multiple times but found no solution.
Does anyone know where I should look to detect the problem?
Thanks!
EDITED added contents of spec/rails_helper.rb updated Wed, 10 Jun 2015
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara/poltergeist'
require 'capybara-screenshot/rspec'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :phantomjs => Phantomjs.path, :inspector => true)
end
Capybara.javascript_driver = :webkit
include Warden::Test::Helpers
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
I had the same issue
Why is FactoryBot not finding our factories only inside RSpec, I do not know.
But making FactoryBot explicitly find my definitions worked.
So you just need to add this to your spec_helper:
FactoryBot.find_definitions
in my case, I followed the general instructions in this answer:
https://stackoverflow.com/a/49436531/8790125
So I create a file spec/support/factory_bot.rb
with this code (note the find_definitions):
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
FactoryBot.find_definitions
end
and then required on rails_helper:
# ...
require 'rspec/rails'
require 'support/factory_bot'
# ...
I am having an issue using the describe syntax in minitest. When I run: ruby -Itest test/elasticsearch/es_record_test.rb
Its only picking up 1 test, and not picking up the one in the describe block.
pass: 1, fail: 0, error: 0, skip: 0
total: 1 tests with 1 assertions in 0.075147 seconds
Below is my current code:
require "test_helper"
class EsRecordTest < Minitest::Spec
let(:id) { '123' }
let(:invalid_id) { '456' }
let(:index_name) { 'es' }
let(:index_type) { 'test' }
let(:body) {{ :body => 'data' }}
before do
Elasticsearch::EsRecord.stub(:index_name, index_name) do
Elasticsearch::EsRecord.stub(:index_type, index_type) do
Elasticsearch::EsRecord.index(id, body)
end
end
end
it "should raise an error for unimplemented methods" do
assert_raises NotImplementedError do
Elasticsearch::EsRecord.index_name
Elasticsearch::EsRecord.index_type
end
end
describe "::delete_index_type" do
it 'should be able to delete an index type if the type exists' do
assert Elasticsearch::EsRecord.delete_index_type(index_name, index_type)
end
end
My test_helper.rb is:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'webmock/minitest'
require 'sidekiq/testing'
require 'typhoeus/adapters/faraday'
WebMock.disable_net_connect!(:allow_localhost => true)
Turn.config.format = :outline
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...
extend MiniTest::Spec::DSL
register_spec_type self do |desc|
desc < ActiveRecord::Base if desc.is_a? Class
end
end
My gemfile:
gem "byebug", group: [:development, :test]
gem 'http_logger', require: true, group: [:development]
gem "minitest-rails", '~> 0.9', group: [:development, :test]
group :test do
gem 'cucumber-rails', :require => false, group: [:test]
gem 'selenium-webdriver', "~> 2.40.0"
gem 'vcr'
gem 'database_cleaner'
gem 'webmock'
gem 'elasticsearch-extensions'
gem 'rspec-rails'
gem 'turn'
gem 'pickle', :git => "https://github.com/zgchurch/pickle.git"
end
Both RSpec and Minitest define the describe method. Since you have rspec-rails added in your Gemfile it is using RSpec. I don't know of a way to have both RSpec and Minitest's Spec DSL activated at the same time.