All Questions
Tagged with rspec rspec-rails
1,561 questions
0
votes
0
answers
43
views
Failure/Error: choice = gets.chomp.upcase NoMethodError: undefined method `chomp' for nil [closed]
I have little experience in ruby development and I can't understand why this error occurs when running tests. Can you help me?
class CashMachine
START_BALANCE = 100.0
def initialize
@balance =...
-2
votes
1
answer
41
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
2
answers
75
views
Rails: Accessing SQL alias attributes declared with ".select"
Let's say in my controller, I use a "where" clause on Teachers so I can put the alias, class_size, on the number of students
@teachers = Teachers.select("teachers.*, count(students.id) ...
1
vote
1
answer
97
views
how to resolve flaky rspecs?
I have this Ruby Rspec randomly failing on my CICD (doesn't fail locally).
This is a minified part of my RSpec:
require "rails_helper"
RSpec.describe "Admin::Vendors", type: :...
0
votes
1
answer
29
views
How do I sign in in a before hook when writing system tests?
I have a Ruby on Rails application with RSpec, Capybara and Selenium with Chromium/Chrome. Because I need to sign in before I can test any application features I visit / first, get redirected to the ...
0
votes
2
answers
110
views
Rspec not able to trigger method inside after_commit callback
I am working on writing a test case for a model Benefit. The class file contains an after_commit callback which calls a method update_contract. It also has belongs_to :contract, touch: true.
@contract ...
0
votes
2
answers
62
views
RSpec How to test when a condition is not met, methods for test verification
"When a customer turns off notifications, notify_on becomes false, and certain conditions are not met. The approach I'm considering is to check whether BellNotify.create! is executed when ...
0
votes
0
answers
56
views
rspec describe/context label within loop not working
Hi there
Because i cannot distinguish if i am wrong or if this is a bug i opened a bug report too.
I need to have a describe/context block within a loop and I am facing a totally strange behavior at ...
2
votes
2
answers
304
views
With Action Cable, test that many messages are broadcast
I know that I can test that an Action Cable message is broadcast like so:
expect {
ActionCable.server.broadcast "messages", text: 'Hi!'
}.to have_broadcasted_to("messages").with(...
1
vote
1
answer
88
views
How to stub out current_user in JWT model for Rspec?
I am trying to stub out the @current_user that gets set in my concern for use in controllers. I am using a concern for authenticating a JWT and setting the @current_user in the concern. The JWTs get ...
0
votes
0
answers
34
views
Update record in test db for rspec
I have a record record with attribute attribute that I want to set to {}, so I've tried
// spec file
record.update_attribute(attribute: {})
record.attribute = {}
// factorybot file
trait :update_attr ...
1
vote
0
answers
130
views
RSpec system test fails when broadcasting later in a background job (ActiveJob and ActionCable)
Using rails 7.1.2 and rspec-rails 6.1.0
TL;TR Tests fail only when broadcasting "later" in background jobs by using config.include ActiveJob::TestHelper in the rails_helper.rb file, but they ...
0
votes
1
answer
56
views
Devise-related system tests fail when run en mass but they pass when run individually (Devise::Mailer)
I'm using RSpec to system test my Ruby on Rails 7 app that uses Devise.
When I run system specs individually then each of them passes, but when I run all system specs globally en mass then the ones ...
0
votes
0
answers
49
views
Rails Rspec tests only intermittently recognizing polymorphic associations
I am in the process of updating a very old legacy app from Rails 3 to Rails 4.2 (I know, I know). Our structure is that we have a models gem, a gem for spec factories, and multiple apps that use both ...
1
vote
1
answer
117
views
Issue with Rspec testing form views with scoped route
After scaffolding a new model for our Ruby on Rails application (Rails version 6.0.6), I got several scaffolded Rspec tests for the new views (index, show, new, and edit) for the new model. I double ...
0
votes
1
answer
22
views
Failing HMT test when checking for "destroy" dependency
I'm currently writing RSpec tests to validate associations in my Rails application. Specifically, I'm trying to ensure that the Artist model has a proper association with Albums through the ...
-1
votes
2
answers
81
views
How to use rspec to test ActiveRecord model scopes and queries
I am trying to write rspec-rails tests on my ActiveRecord Flight object scopes (e.g. #around_date). Here's one test I've written:
let(:flight) { create(:flight) }
date = DateTime.new(year=2023, ...
1
vote
1
answer
113
views
RSpec how is be_invalid matcher made available
I noticed following line
expect(actual).to be_invalid
while I was looking at https://github.com/perfectline/validates_url/blob/81ec1516423af0b4fdc7cabbcda0089e434f2703/lib/validate_url/rspec_matcher....
0
votes
1
answer
447
views
Tests: How to check if a sidekiq job enqueued another sidekiq job
I have this two example class, and I want to write a rspec that runs PaymentCheckJob for first time, and if payment_type == 'ticket', I want to check if it enqueued another sidekiq job
class ...
1
vote
0
answers
36
views
No route matches [GET] error in feature spec for update button routed to PUT
I'm writing a feature test for a Rails 7 application. I'm using RSpec, Capybara, and Capybara-Webkit.
The feature I'm testing archives a record(a developer app) when the user clicks an 'Archive' ...
0
votes
0
answers
75
views
RoR, How can I unit test controllers using RSPEC which are protected by JWT authentication system?
I'm trying to unit test RoR api controllers using rspec-rails gem.
All my controllers are implemented this ProtectedController class which intercept all request and validate the authorization header
...
0
votes
2
answers
214
views
Rspec stubbing a constant set to a Rails credential
I have a class:
class Vendor::Connection
VENDOR_CLIENT_ID = Rails.application.credentials.vendor_api[:client_id].freeze
VENDOR_CLIENT_SECRET = Rails.application.credentials.vendor_api[:...
0
votes
1
answer
20
views
RSpec Request spec setup issue
I have the following spec:
it "allows a valid request" do
user = create :user
headers = {"Authorization" => "Bearer #{user.api_token}"}
get "/api/v1/user&...
0
votes
1
answer
132
views
Accessing ENV variable in spec helper
Is it an expected behavior that with rspec-rails (6.0.3) at the top of spec_helper.rb I'm not able to get my environment variables?
I'm trying to set simplecov to run only on CI and, as I have done ...
1
vote
0
answers
129
views
ActiveRecord::RecordNotUnique error when running RSpec test for association creation
I'm encountering an issue while running RSpec test cases for my Rails application. The test is related to creating associations between models. Specifically, I have a test that involves creating an ...
0
votes
0
answers
326
views
How to RSpec system test an Active Storage image?
I'm writing RSpec system specs to test the presence in pages of Active Storage-powered images. Capybara, Selenium & Co. with a real (non-headless) browser are used.
Actually in my spec file I'm ...
1
vote
1
answer
846
views
Testing Ruby on Rails ActiveStorage direct upload by running S3 locally: authentication errors when trying to connect to MinIO
I would like to use S3-like storage also for my tests, and so I downloaded and installed MinIO on my machine. However when I run bin/bundle exec rspec --fail-fast spec/models/hanzi_spec.rb, I get ...
0
votes
1
answer
155
views
Setting a tenant before making calls to create object in Rspec
We are using Rails 7
We are trying to test some objects that are tenant'ed. So, we need to create a tenant and set that tenant before any other objects are created. I am unsure of how to structure ...
0
votes
1
answer
53
views
Rails RSpec fails when trying to test a model within a controller spec
I have searched a lot to see if there is a solution to this issue, I have not seen anyone reporting this issue, let me add that I am a rails novice.
Ruby Version: 3.2.2
Rails Version: 7.0.5.1
Rspec ...
1
vote
1
answer
128
views
Rspec returning expeced 1 time received 0 time
My rspec test case is returning
expected: 1 time with any arguments
received: 0 times with any arguments
this error, even though the method gets called by the specific active record.
here is my rspec:...
-1
votes
1
answer
413
views
How to override model's default attributes using FactoryBot?
I have an Order model with a default attribute status_id whose default value is 1.
I use rspec for testing my rails app. I create an object in a describe block as follows:
let!(:order) { create(:order,...
-2
votes
1
answer
194
views
How can I write rspec to work for this admin panel feature in Ruby on Rails?
Can anyone tell me, how to write rspec for this admin panel feature in ruby on rails.
ActiveAdmin.register BxBlockCfappcoinsmanagement::CoinSetting, as: "Settings" do
menu false
...
1
vote
0
answers
89
views
Having problem testing a form submission with attached file with Rspec and Capybara
Ruby in Rails.
I have a form where can upload an image in a file type input field along with other 3 text fields.
Everything works like a charm in production, development and so.
BUT when I run a test ...
0
votes
1
answer
286
views
PG::InvalidParameterValue: ERROR: time zone "Central Time (US & Canada)" not recognized - Error updating Rails timezone in RSpec test
I'm trying to update a time zone as part of a spec to test one of the scenarios which seems to be an edge case that's not working.
I have a timezone attribute for one of the tables and its of type ...
0
votes
1
answer
392
views
Strange problem when running system tests as a whole with RSpec and Capybara/Selenium
I've regular RSpec Rails system tests using Capybara/Selenium, like this:
RSpec.describe "Model", type: :system do
context "update model info1" do
# ...
end
context &...
0
votes
1
answer
224
views
Matching text having HTML tags with RSpec and Capybara
I'm using RSpec and Capybara to expect text containing HTML tags, like this:
expect(page).to have_css('#dom_id', :text => "Text with <b>HTML tags</b>", :visible => true, :...
0
votes
0
answers
73
views
RSpec api json recovers html instead of json
Context:
I have created a timesheet, I want to test the CRUD of it with the json api, but when I launch the test in the console it returns me an error indicating the webpacker can't find the file.
...
1
vote
2
answers
623
views
Rspec fails after Rails version update
I got into a new project that hasn't been touched for a couple of years prior, the clients want me and my team to update Ruby/Rails from Rails 4 to the newest possible version without breaking the way ...
0
votes
0
answers
304
views
Working with rails associations in rspec - associations methods behave unpredictably
Issue:
When i define a variable in a spec and then call an association methods they don't seem to consistently query the database. Some seem stuck. I would be really greatful for any help on this, as ...
2
votes
0
answers
253
views
Test rails controller concern in RSpec request spec
Say we have a controller concern like this:
module BillingErrorRescuer
extend ActiveSupport::Concern
included do
rescue_from Billing::LimitExceeded, with: :rescue_billing_limits
def ...
3
votes
1
answer
2k
views
Set Session in RSpec with Rails 7 API
I am trying to create a logic where a user gets an invitation from a group. If they signup via that invitation, the group id will be stored in the session and when they signup, they will be ...
0
votes
1
answer
1k
views
RSpec compare Job enqueued for a later time
I have a job called DisableInvitationJob. I call it like this:
DisableInvitationJob.set(wait: 7.days).perform_later(invitation: @invitation)
And when testing the call with RSpec:
it {
expect(...
0
votes
2
answers
559
views
Rails tests: Net::HTTP post - empty response.body in Rspec when fetching external site
Strange RSpec behaviour: I send a post request from a test.
context "when webmaster" do
let(:question) { create(:question) }
let(:user) { create(:user, :webmaster) }
describe &...
0
votes
0
answers
135
views
How to pass params from rails RSpec view
I have a view spec under spec/views/interests/show.html.slim_spec.rb
require 'rails_helper'
RSpec.describe "interests/show", type: :view do
interest = FactoryBot.create(:interest)
...
0
votes
1
answer
82
views
Capybara::ElementNotFound: Unable to find field "post[date]" that is not disabled
I am getting errors while doing integration tests with rspec and the problem here lies with the fill_in , can anyone explain me why this is happening??
require 'rails_helper'
describe 'navigate' do
...
2
votes
1
answer
95
views
What logic behind rspec-rails generates non-REST description like `POST /create`
rspec-rails provides scaffold generators for request specs. It generates following code:
RSpec.describe "/widgets", type: :request do
# ...
describe "POST /create" do
...
-1
votes
1
answer
103
views
Rspec Arrays not working as they should be?
The test should update the item (which is already created) with the tag_id that was placed into the tag_id parameter. I know that I can do this because I have tried it on postman and it works every ...
0
votes
1
answer
82
views
Rspec is failing for request header
I am writing a code which will call some 3rd party API. So, I am using the API_HELPER module to call the 3rd party api.
module ApiHelper
def self.get_header(header)
@request['Accept'] = ...
4
votes
2
answers
2k
views
How to properly mock an instance of an Active Record model to test that a method has been called
With the following controller action:
def create
my_foo = MyFoo.find params[:foo_id]
my_bar = my_foo.my_bars.create! my_bar_params
my_bar.send_notifications
redirect_to my_bar
end
In my ...
5
votes
1
answer
722
views
How to configure Sorbet with rspec?
I have a simple test but the describe keyword is not working in Sorbet tests.
The error I'm receiving on these methods:
Method `describe` does not exist on `T.class_of(<root>)`7003
RSpec....