18,274 questions
2
votes
1
answer
70
views
Stub a method on a Thor CLI
I have a Thor class that has a getter method defined like this:
# Playgrounds CLI skeleton class.
# Commands are added from commands folder
class CLI < Thor
def self.exit_on_failure?
true
...
2
votes
0
answers
88
views
How to feed gets to avoid manual input during test execution? [duplicate]
I have a problem with one of my class methods.
class Input
# Method to get a valid number of days to assign prices to
def Input.get_valid_number_of_days
puts "Time to enter the number of ...
1
vote
1
answer
95
views
How does a GitHub Actions workflow notice that a unit test has failed?
Finished in 0.0137 seconds (files took 0.0769 seconds to load)
5 examples, 1 failure
Failed examples:
rspec ./spec/basic_math_spec.rb:10 # Basic-Math Subtract numbers
Error: Process completed with ...
0
votes
1
answer
79
views
default_driver being ignored by Capybara in Rails/RSpec tests
I am hoping someone can point out what I am doing wrong. I wanted to configure RSpec/Capybara to do my system tests such that the default behaviour would be to run headless, but if I set js:true on ...
1
vote
1
answer
64
views
How to sign in a user and create associated records with factories when testing
I have several tables in my application which are associated with a user. In order to run my integration tests the application first needs to log in the test user.
config.before(:each, :type => ...
3
votes
2
answers
126
views
Ignoring exit when running Rspec
In Ruby, I need to print a feedback message and then exit with a status code of 1.
class Foo
def bar
puts "error"
exit 1
end
end
In RSpec, how can I ignore exit 1 when expecting ...
0
votes
1
answer
27
views
Set action_mailer.default_url_options and routes.default_url_options in Rails test environment
I've seen a lot of questions about errors concerning action_mailer.default_url_options and default_url_options[:host] during system tests.
Below I share a good solution I found.
0
votes
0
answers
49
views
Rails 7.1.5 `touch: true` causes StackLevelTooDeep with `delegated_type`
In a Rails app for managing course bookings, I want to keep the updated_at timestamps of related models in sync.
Here's the setup:
class ShopOrder < ApplicationRecord
delegated_type :...
1
vote
0
answers
47
views
Caypbara/rspec throwing both `Capybara::ModalNotFound` and `Selenium::WebDriver::Error::UnexpectedAlertOpenError` in the same test
So i have a "Delete" link that brings up a html confirm modal. In my rspec system test though, it keeps erroring out with both errror messages Capybara::ModalNotFound and Selenium::WebDriver:...
0
votes
0
answers
79
views
RSpec -- re-evaluate allow .and_yield when mocked method is called
In this code, sync_external accepts an array of posts and modifies it by calling a sync method from an external module ExternalService, which is mocked in the test.
Assume there's some reason to pass ...
0
votes
1
answer
47
views
New method of Rails controller being called by Rspec test
I have a Rails controller I'm trying to write a new method for, and I'm running into an issue writing the tests for it. I've simplified the methods and tests repeatedly just trying to get something to ...
1
vote
2
answers
176
views
How to fix error in Rails Authentication generation with RSpec?
I created a new Rails 8 application. I installed RSpec for testing.
$ rails g authentication
invoke erb
create app/views/passwords/new.html.erb
create app/views/passwords/edit....
1
vote
0
answers
100
views
RSpec system specs fail without allowing JS to finish
I'm having an issue running my RSpec suite. The suite is "old," and everything worked fine until a few days ago when system specs started to fail without an apparent reason.
All the failures ...
0
votes
1
answer
76
views
Iterating to create contexts from a class
I have an interesting problem, and do not know if I am overthinking something, or just missing the big picture.
I have a Rspec feature test iterating over each possible “user flow”. Each “user flow” ...
0
votes
1
answer
50
views
Rspec: testing "rescue_from" on type: :request
I have a Rails 6.1 controller that sets:
# File: app/controllers/foo_controller.rb
rescue_from Exception, with: :error_handler
My Rspec 6.1 test (is a "request" type) is like:
# File: spec/...
1
vote
0
answers
290
views
Does the removal of the Spring gem in Rails 8 cause slower RSpec tests?
I migrated Rails from version 7 to 8 and I noticed that the Spring gem was removed from Rails 8. I suspect this might be someway contributing to my RSpec test suite running significantly slower, ...
0
votes
1
answer
184
views
Rspec - How to replace receive by have_received when dealing with an expect within a block?
I have the following Ruby class I want to test:
class Logger
# ...
def process
X.process
rescue StandardError => e
::Rails.logger.error('Progname') { 'Could not produce the audit log'...
0
votes
1
answer
120
views
How to make helper_methods available to Views in Rspec
We have some helper_methods in our controllers that are used across both the controller and the view they render.
For example:
helper_method :example_method
def example_method
@example_method ||= ...
1
vote
2
answers
111
views
Rails acceptance validation always correct
I have this Rails 6.1 model:
class BankAccount < ApplicationRecord
belongs_to :donor
before_validation -> {
puts #AccAuth: #{account_authorization}"
}
validates :...
0
votes
1
answer
37
views
Testing spec helper: how to expect a failure in an after hook
I've got a spec helper that, in an after hook, checks the output of a method call. The hook itself just calls a method from an included module, and I've tested all that successfully.
Now I want to ...
-1
votes
1
answer
99
views
What is alternative of `stripe-ruby-mock` to test the latest (v15) Stripe API?
Currently, we are using the stripe-ruby-mock gem in our application for testing Stripe APIs Integration, along with Stripe version '2015-04-07'. As we proceed with the Stripe upgrade and aim to ...
1
vote
1
answer
142
views
How to write an RSpec test to cover a private method in Rails?
I'm having an issue with SonarQube and I would like to know if anyone can help me. My application can only pass if at least 80% of the code is covered by tests.
In my controller, I have the cancel ...
1
vote
0
answers
62
views
Devise gem error in test environment: serialize_from_session gives error "wrong number of arguments (given 10, expected 2)"
I just updated my app to rails 8 with the newest version of Devise and hotwire turbo. When running tests with Capybara/Rspec I'm seeing certain form submissions fail with a 500 error from inside of ...
1
vote
0
answers
40
views
Is there a way to take a screenshot when before or after :context hook fails in rspec
I've implemented screenshot capturing for my automation project using rspec/capybara.
I was expecting capybara to take a screenshot each time a test fails, no matter where it fails (in preconditions ...
1
vote
1
answer
133
views
Rspec - Uniqueness shouldamatcher failing
I have an ActiveRecord table with a column called name. I need this field to be present and unique. So I have the following specs
it { should validate_presence_of(:name) }
it { should ...
0
votes
1
answer
179
views
Deprecation Warning: "Bolding log text with a positional boolean is deprecated" when running Rails tests with Rspec
When running my tests in a Rails application, I see the following deprecation warning multiple times:
DEPRECATION WARNING: Bolding log text with a positional boolean is deprecated and will be removed ...
0
votes
1
answer
105
views
Is there a way to set up an expectation for a hash but being "indifferent" about whether keys are symbols or strings?
Working with rspec-expectations 3.13.3 and have code such as:
def methname(**kwargs)
# let's assume this method doesn't mind whether the kwargs are keyed by symbols or strings
p(received: kwargs)
...
1
vote
1
answer
76
views
How to test a method call with keyword arguments not including a particular keyword
I have the following method:
def clone(branch: nil, depth: nil)
...
end
For a particular test case, I want to test that the method is called without the branch argument. In Ruby 2.x the following ...
0
votes
3
answers
173
views
In RSpec, how to mock a method so that it returns its argument
Let's say I have a class with a method that modifies and returns a hash. When testing the class the method will be called, but for the purposes of the test, I want it to return its argument ...
0
votes
0
answers
92
views
Expect method not to be called by default
Let's say there's a method report defined on ErrorReporter module.
In my RSpec test suite I'd like to somehow globally stub this method so that test fails if it's not explicitly expected to receive ...
0
votes
2
answers
116
views
Rails - Named route not working in controller spec
In my routes.rb file I had this route:
resources :profiles
In my controller specs I had thus paths such as:
profiles_path
new_profile_path
etc.
Now I had to move this inside a namespace. The new ...
-1
votes
2
answers
493
views
How do I install factory_bot_rails? I am using Rspec
I am trying to install factory_bot_rails to use with rspec.
I am working my way through Everyday Rails Testing with RSpec A practical approach to test-driven development, by Aaron Sumner, http://...
1
vote
2
answers
73
views
Testing valid factory when belongs_to
In my Rails application I have a model Profile that belongs_to a User as such:
class User < ApplicationRecord
has_many :profiles
end
class Profile < ApplicationRecord
belongs_to :user
...
1
vote
0
answers
79
views
API test fails but works fine with GraphiQL route (Header issue?)
I'm working on a Rails API with GraphQL, and I'm having issues with testing a mutation using RSpec. When I use the /graphiql route and provide the required headers manually, everything works perfectly....
0
votes
3
answers
93
views
Simplecov wants me to test controller assignments, is there a way to do this without the rails-controller-testing gem?
For a very basic controller action such as:
def new
@user = User.new
end
Simplecov wants this assignment tested. I can't think of anyway to test it other than using the rails-controller-testing gem ...
0
votes
0
answers
51
views
Rspec system test for mobile using responsive web layout - how to specify width lower than 500px using chromedriver?
I'm trying to write an RSpec system spec to test working functionality through a responsive web layout that could emulate how one would use things in a mobile.
How do I get the window size to have a ...
-2
votes
1
answer
54
views
Testing Active Admin Page with RSpec
In my Rails application, ruby 2.6.5, rails 6.1. I use Active admin and I created this page. How can I create some tests for this using rspec?
I have already created tests for the model and the ...
0
votes
1
answer
131
views
Error using Selenium WebDriver and Capybara in Rails application
I'm using rails 6.1 with ruby 2.6.5 and I’m creating tests for an Active Admin new page, The best way I found to test this new page was by using the gems Capybara, Webdrivers, and Selenium-Webdrivers. ...
1
vote
2
answers
370
views
GET request JSON body params in rswag specs
Issue
How can I write a rswag spec that would pass my_field: 'my_value' into my controller's params for a GET request?
Initial attempt
config/routes.rb:
Rails.application.routes.draw do
namespace :...
1
vote
0
answers
78
views
RSpec expect(...).to receive(...).with(...) return error unexpected arguments with same variables
class Foo
def fizz(user)
puts user
end
end
let(:user){create(:user)}
it do
expect(Foo).to receive(:fizz).with(user)
end
I run this test in RSpec of Rails app. Test fails with below ...
0
votes
1
answer
81
views
RSpec error with expecting DateTime value to be_within a duration of a ActiveSupport::TimeWithZone value
Why does the following test give me a TypeError: expected numeric error:
it { expect(DateTime.now).to be_within(1.second).of Time.zone.now }
but this test does not?
it { expect(DateTime.now).to ...
1
vote
0
answers
25
views
My Rspec Test Isn't Following The Range I Specified?
I'm running into trouble with my test. Here it is. It performs a number of inserts into a C database db. The problem is that my test doesn't seem to actually do the number of inserts.
If I specify the ...
0
votes
0
answers
190
views
CircleCI Parallelism messing up with time-based tests
We have a big old build that is taking too long to run tests and Im trying to setup parallelism to speed things up. After the configuration, I can see the tests are indeed running in parallel and are ...
1
vote
3
answers
112
views
ISO 8061 built-in support in RSpec or Minitest
When to_json is called on an object in Rails, the datetime objects are converted to the ISO 8601 standard automatically which is an excellent tool for sending data back to a client.
[43] pry(main)> ...
2
votes
2
answers
144
views
RSwag not supporting apiKey pairs in requests
As you can see from the github issue, RSwag does not support multiple headers for api Key authentication.
This is my openapi/swagger yml, in following with Multiple API Key Setup in swagger docs:
...
0
votes
1
answer
100
views
When do raise_errors_for_deprecations! and raise_on_warning trigger?
Ruby: 3.4.0-preview1
rspec: 3.13.0
.rspec
--warning
spec_helper.rb
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'rspec/its'
RSpec.configure do |config|
config....
0
votes
1
answer
78
views
Why do Active Storage tests hang?
I have installed this gem to validate and test active storage:
gem 'active_storage_validations'
gem 'shoulda-matchers', '~> 6.0'
# app/models/asset.rb
class Asset < ApplicationRecord
...
0
votes
1
answer
86
views
How to test active storage dimension validation?
I have installed this gem to validate and test active storage:
gem 'active_storage_validations'
# Asset Model
class Asset < ApplicationRecord
has_one_attached :portrait
validates(
:...
1
vote
1
answer
85
views
Ruby rspec expectations: Is there any way to ensure a keyword expectation is not "bogus"?
Let's say I have:
class Foo
def do_thing(arg_one:, arg_two:)
# ...
end
end
class Bar
def initialize(foo)
@foo = foo
end
def do_delegated_thing
@foo.do_thing(arg_one: "x&...
0
votes
1
answer
183
views
Capybara does not visit routes inside rails 7.2 application
I have always used Capybara with Rspec and with Rails 4, 5, and 6 - inside Docker containers without Selenium - to run end-to-end tests.
Now, with Rails 7.2, the same tests fail because Capybara, ...