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.
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 have recently upgraded my ruby on rails project to rails 5.0.7 and ruby 2.5.1 and I am getting an RSPEC error undefined methodexpects' for` different objects I am testing.
I have tried adding a configuration in the spec_helper.rb file as suggested here (although I did a quick search and didn't find :should defined anywhere), here and here:
config.expect_with :rspec do |expectations|
expectations.syntax = [:expect, :expects]
end
Also tried including include RSpec::Matchers in my spec_helper.rb, but then I get even more errors:
`only the `receive`, `have_received` and `receive_messages` matchers are supported with `expect(...).to`, but you have provided: #<RSpec::Matchers::BuiltIn::Eq:0x00007f962b3db058>`
An example of how I am using expect:
describe "process" do
before :each do
#item = Item.new
end
it "parses the uploaded file and extract the correct report data" do
item_a_data = {"name" => "one"}
item_b_data = {"name" => "two"}
file_contents = {"items" => [item_a_data, item_b_data]}
#item.data_file = double("file", :read => file_contents.to_json)
#item.name = item_a_data["name"]
#item.expects(:interpret_json_file).with(#item.data_file).returns(file_contents)
#item.expects(:save).returns(true)
expect(#item.process_data_file).to be_truthy
expect(#item.data).to eq item_a_data.to_json
end
Notice that the error occurs when I call #item.expects
In my Gemfile I have the following gems (among others):
group :development, :test do
gem 'byebug'
gem 'sqlite3'
gem 'rspec-rails'
gem 'rb-fsevent', '~> 0.9.1'
gem 'guard-rspec', '4.7.3'
gem 'guard-spork', '2.1.0'
gem 'spork', '0.9.2'
gem 'jasmine-rails'
gem 'teaspoon-jasmine'
end
group :test do
gem 'capybara'
gem 'cucumber-rails', :require => false
gem 'database_cleaner'
gem 'factory_girl_rails', '4.1.0'
gem 'launchy'
gem 'poltergeist'
gem 'timecop'
gem 'webmock'
gem 'simplecov', '~> 0.16.0'
gem 'rails-controller-testing'
end
spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
require 'simplecov'
SimpleCov.start 'rails'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'devise'
require './spec/controllers/controller_helpers.rb'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include ControllerHelpers, type: :controller
config.include Capybara::DSL
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include FactoryGirl::Syntax::Methods
# config.expect_with :rspec do |expectations|
# expectations.syntax = :expect
# end
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
The solution would be to re-write it to the following syntax:
expect(#item).to receive(:interpret_json_file).with(#item.data_file) { file_contents }
expect(#item).to receive(:save) { true }
Basically ActiveRecord model does not know about rspec expectations and it should not. If that specs were working before then you might used some decoration for ActiveRecord models and that is why it worked previously.
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) }
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 will use RSpec with Factory girl in my Rails3 Project. I have installed factory girl but it don't find the factorys i have this error
Failure/Error: Factory.build(:user).should_be valid
No such factory: user
spec/factories/user_factory.rb :
Factory.define :user do |u|
u.username 'otto'
end
spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
Gemfile:
group :development, :test do
gem 'webrat'
gem "cucumber-rails"
gem "rspec-rails"
gem "rspec"
gem "autotest"
gem 'factory_girl'
end
Thanks
Do you have the following lines in your config\application.rb:
# Configure generators values.
config.generators do |g|
g.test_framework :rspec, :fixture => true
g.fixture_replacement :factory_girl, :dir=>"spec/factories"
end
Add the 'factory_girl_rails" gem to your Gemfile under your :test, :development groups, as follows:
group :development, :test do
gem 'webrat'
gem "cucumber-rails"
gem "rspec-rails"
gem "rspec"
gem "autotest"
gem 'factory_girl'
gem 'factory_girl_rails'
end
In Rails 3, you need to add that gem to make it work. Hope it helps.
have you tried adding something like
Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}
in the spec_helper?
that did it for me
If your rspec config is loading everything under spec/support then you can put your factories dir there.
See this post: http://www.codeography.com/2010/04/30/using-factory_girl-and-rspec-with-rails-3.html