Questions tagged [rspec-rails]

rspec-rails is an extension of the RSpec testing framework that provides support for testing Ruby on Rails applications.

rspec-rails
Filter by
Sorted by
Tagged with
0 votes
1 answer
22 views

In rspec request specs how to test the actual behavior of endpoints being called with non-ascii characters in the URL?

When writing a test for rspec that calls an endpoint that has a non-ascii character, the test throws InvalidURIError - this is expected because non-ascii characters are not valid in URIs. However the ...
0 votes
2 answers
40 views

rails 7, Active Storage, Factory Bot, freeze my RSPECs

After a lot hours tried to resolve by myself this problem. Ruby 3.3.0 Rails 7.1.3.2 factory_bot 6.4.6 mini_magick 4.12.0 rspec-rails 6.1.2 OSX All works very well when I do it everything in browsers....
2 votes
2 answers
69 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
30 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
22 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 ...
0 votes
0 answers
17 views

How to click on a Bootstrap 5 range field at a given positional value with Capybara-Selenium?

I'm using Bootstrap 5.3 range min-max field and RSpec Rails Capybara with Selenium. In my RSpec Rails system test, I would like to simulate a click on a "positional value", say positional ...
37 votes
2 answers
11k views

Show runtime for each rspec example

currently I'm running more than 1k examples and it's taking a long time to complete (more than 20 minutes!!!). I'd like to identify which examples are the ones taking more time to complete, is there ...
0 votes
2 answers
589 views

Rails/Rspec - How to use multiple it without redoing work

Multiple times I want to check multiple things over the same action, say that I want to do a PUT and see if the request returned 200 and if the response value contains some values and if the ...
1 vote
3 answers
328 views

Configure screenshots file names for Rails5/RSpec

It looks like for Rails5/RSpec systems tests, the default is to use selenium-webdriver gem to take screenshots when a spec fails. Is there a way to configure the format of the filename for the screen ...
2 votes
2 answers
6k views

Sidekiq::Testing.fake! not faking Sidekiq::Queue

I have a simple worker that is accessing the size of its own queue: require 'sidekiq/api' class TestWorker include Sidekiq::Worker def perform(*args) Sidekiq::Queue.new('test').size end ...
6 votes
2 answers
4k views

Parallel testing in rails 6 and rspec

I am trying to make parallel tests using rspec and rails 6. According to their documentation, it is just required to add PARALLEL_WORKERS=15 where 15 is the number of workers. However, this works for ...
2 votes
2 answers
236 views

Params is being nested in params in an Rspec post request

I am currently testing a post request for a nested route with Rspec request. It is set up like this: describe 'POST /url_contents' do let!(:role) { create(:role, name: "admin") } let(:...
1 vote
0 answers
190 views

ActionView::Template::Error: while running rspec in groups or file

I am working on view specs using RSpec. When I use render in it block. Then my first test case is passed but 2nd one is saying the error. Failure/Error: render ActionView::Template::Error: ...
0 votes
3 answers
4k views

How to configure FactoryBot files path in a Rails engine?

For my first Ruby on Rails engine - named "glossary" - I wish to implement Rspec, ShouldaMatchers, FactoryBot as test suite. Things look good, but FactoryBot keeps claiming that factories are not ...
1 vote
0 answers
49 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
33 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
73 views

Rails migration error 'wrong number of arguments'

I'm unable to migrate, getting this error: StandardError: An error has occurred, this and all later migrations canceled: Caused by: ArgumentError: wrong number of arguments (given 2, expected 1) /...
3 votes
1 answer
3k views

Rspec testing for comment model ( Validation failed: User must exist)

I have a association for comment and the user model ( belongs_to :user and has_many :comments). in the comment_spec it throwing an error. comment model: class Comment < ApplicationRecord ...
1 vote
1 answer
42 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
0 answers
69 views

spec failed. NoMethodError: undefined method `callback' for #<RSpec::ExampleGroups::O

I want to run callbacks. I have added gem 'shoulda-callback-matchers' gem 'shoulda-matchers' in gem file. My code for callbacks is describe "#callbacks" do it { is_expected.to ...
0 votes
0 answers
39 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 ...
0 votes
1 answer
36 views

ActiveRecord includes not throwing exception in Rspec?

When i try to add the some random string in active record includes is not failing instead returning the empty relation in rspec. In development env Same is throwing error in dev rails c
0 votes
0 answers
40 views

Factorybot created Instance not existing until I reference it

I am writing tests in Rspec with Factorybot. When my test looks like this, it fails: describe "The #add_admin function" do context "with a normal user" do let(:user) { ...
3 votes
3 answers
4k views

RSpec test to expect only certain properties of an ActiveRecord model to change

I am successfully testing whether certain properties of an ActiveRecord model are updated. I also want to test that ONLY those properties have changed. I was hoping I could hook into the model's ....
3 votes
2 answers
709 views

Best way to deal with environment-sourced constants when rspec testing?

I have a model that has a constant that looks like: SOURCE_QUOTA = { 'free' => (ENV['FREE_SOURCE_QUOTA'] || '5').to_i, 'premium' => (ENV['PREMIUM_SOURCE_QUOTA'] || '100').to_i }....
0 votes
1 answer
21 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
63 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, ...
7 votes
3 answers
11k views

Rspec : is there a matcher to match array of arrays, not testing order

I try to test if two arrays of arrays contain same elements, without testing order of elements. (Rails 5.2 / Rspec-rails 3.8.2) exemple : [['a1', 'a2'], ['b1', 'b2']] [['b2', 'b1'], ['a2', 'a1']] I ...
3 votes
2 answers
2k views

How can I view the rendered page in an RSpec/Capybara view spec?

I know in a normal feature spec using Capybara, I could do something like this: require "rails_helper" describe "users/show", type: :feature do it 'renders a page' do visit user_path ...
0 votes
1 answer
62 views

Selenium::WebDriver::Error::UnexpectedAlertOpenError:

I get an error because an alert is open. And I think I handle it in the right way ;-) According to my research the 'accept_confirm' block is the way to handle this. feature "Users can delete ...
1 vote
1 answer
893 views

uninitialized constant RSpec::Matchers::Composable

I have a problem running the test case which says <class:Matcher>': uninitialized constant RSpec::Matchers::Composable (NameError) after loading so many stuff.Please help! Thanks in advance!
0 votes
1 answer
23 views

My Rails Controller Spec is Not Passing When a Redirect Happens

I'm using rails 6 and ruby 3.1.1 and Rspec It's been a minute since I've written tests, so the syntax is a bit new to me, but "I think" I understand it. A controller create test is not ...
16 votes
6 answers
15k views

How to add "config.include FactoryBot::Syntax::Methods" to rspec config block in spec_helper.rb?

If I add: config.include FactoryBot::Syntax::Methods under RSpec.configure do |config| and run rspec, I see this error: /Users/perry_mac/rails_projects/mymri/spec/spec_helper.rb:21:in `...
1 vote
2 answers
2k views

How can I add a function to FactoryBot globally?

I have a class that extends FactoryBot to include functionality to copy Rails' .first_or_create. module FactoryBotFirstOrCreate def first(type, args) klass = type.to_s.camelize.constantize ...
1 vote
1 answer
61 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....
11 votes
4 answers
5k views

RSpec request spec post an empty array

I am current developing an API endpoint in rails. I want to be sure the endpoint response with the correct error status if the data I need is invalid. I need an array of ids. One of the invalid values ...
0 votes
1 answer
114 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 ...
19 votes
6 answers
11k views

How to properly test ActiveJob's retry_on method with rspec?

I have been attempting to test this method for the past few days with no luck. Another thing I'd like to be able to do is rescue the error that bubbles up after the final retry attempt is made. ...
1 vote
0 answers
33 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' ...
24 votes
3 answers
45k views

ActionController::UrlGenerationError, No route matches

I've read through every similar question I could find and still can't figure out my problem. # routes.rb Rails.application.routes.draw do resources :lists, only: [:index, :show, :create, :update, :...
0 votes
0 answers
61 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
119 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
19 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&...
4 votes
2 answers
1k views

Seeded database key values conflicting with factory bot create/build

Our rails database is pre-seeded with some data for a few of our static models. For example, we have a DocumentType model that gets populated/updated through db/seeds.rb. Users can't modify this ...
0 votes
1 answer
85 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 ...
0 votes
1 answer
72 views

How to write Test case for uniqueness with Scope in Rails Rspec

I want to write RSpec test for this validation but it always give me error validates :post_id, uniqueness: { scope: :user_id } I am using this solution but it is not working it { is_expected.to ...
1 vote
2 answers
743 views

Expect a Turbo Stream Broadcast in an RSpec Request Test

I'm writing a request test in RSpec and in the course of the controller path I am testing, the application sends out two turbo stream broadcasts. I have not been able to get the broadcast_to ...
0 votes
0 answers
15 views

Grape Put endpoint not properly getting the id from Rspec test

I'm trying to write a put endpoint in grape for my api, I wrote a test for it and when I run the test, I get a 400 as a response that says ActiveRecord::RecordNotFound: Couldn't find Book with 'id'=:...
0 votes
2 answers
1k views

click on a image link with Capybara

I am trying to click on a image link with a Capybara / Rspec test. I am having very little success at the moment. I am trying to select the link with href "/post/3", (knowing that they are other ...
1 vote
0 answers
89 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 ...

1
2 3 4 5
50