Questions tagged [rspec]

1

votes
1

answer
256

Views

Rails 4: PG::InFailedSqlTransaction when testing a rake task with RSpec

I am currently trying to test a rake task with RSpec. My Rails version is 4.2.4 and rspec-rails version is 3.3.2. I've have the following in rails_helper.rb: ENV['RAILS_ENV'] ||= 'test' require 'spec_helper' require File.expand_path('../../config/environment', __FILE__) require 'rspec/rails' require...
DaniG2k
1

votes
0

answer
59

Views

Using RSpec to loop through multiple spec groups

I'm somewhat new to this so bear with. I'm trying to execute several groups of tests independently via RSpec::Core # Example spec_groups = [['spec', 'spec2'], ['spec3', 'spec4']] spec_groups.each do |spec_group| RSpec::Core::Runner.run([spec_group]) RSpec.clear_examples end Two groups of specs - I w...
user3761590
1

votes
2

answer
400

Views

Rails 5 rspec test failing for uploaded file stub

In my rspec for uploaded files I am stubbing a function like this: allow(ABCCLass::XYZProcessor).to receive(:prepare_processing).with(csv_statements).and_return(nil) and let(:csv_statements) do [fixture_file_upload('transaction.csv', 'application/csv')] end I get error in expect: expected Rack::Tes...
Shehbaz
1

votes
1

answer
124

Views

Factory for database view in factory_girl

I am using Rails 5.0.2 with factory_girl_rails and RSpec. I have 3 associative models like this: /app/models/ticket.rb class Ticket < ActiveRecord::Base has_many :ticket_snapshots, foreign_key: 'duckduck_ticket_id' end /app/models/ticket_snapshot.rb class TicketSnapshot < ActiveRecord::Base belongs_...
Bijan Hoomand
1

votes
1

answer
30

Views

Ensure label belongs to its checkbox

I have a checkbox and a label for it. Now I want to ensure that the checkbox belongs to its label, like checkbox1 -> label1. How to do it on capybara?
gilbertlcsndle
1

votes
0

answer
181

Views

RSpec Stubbing Java Static Method ArgumentError

I am attempting to stub a Java static method in my specs for my JRuby class which imports some Java libraries. I get the following error for the method call: Failure/Error: JCoDestinationManager.getDestination('properties') ArgumentError: Wron...
mltd
1

votes
0

answer
54

Views

Rails 4.2 - Failing to stub delayed response for image with puffing billy - image not cached and new http connections are disabled

I have rspec tests and I feel there is a bug with puffing billy or maybe I am misunderstanding the use of whitelists in the settings. Basically the test is about checking that, if a third party hosted image (on a service called Cloudinary) takes time to be downloaded by a page, then the user sees a...
Mathieu
1

votes
1

answer
343

Views

Rspec and commit callback giving an incorrect result

I have a User and Role model: class User < ApplicationRecord has_and_belongs_to_many :roles validates :roles, presence: true, on: :save after_create_commit :assign_role private def assign_role self.roles { where(name: :client) } end I can't get the following Rspec to pass: describe '#assign_role' d...
chell
1

votes
0

answer
55

Views

Rspec response changes inbetween calls

I'm using the below line of code to check that a controller renders a response with parameters expect(controller).to receive(:render).with('orders/order_pickup/layout', {:locals=>{:state=>'pickup'}}) But when I run the rspec test, I get... Diff: @@ -1,2 +1,2 @@ -['orders/order_pickup/layout', {:loca...
Patrick Stetz
1

votes
1

answer
512

Views

Devise & Factorybot authentication for RSpec POST request

Setup using devise_token_auth, Factorygirl, and RSpec. Trying to sign in a user, and make a post request, but I am getting 401 unauthorized response. /spec/controllers/scripts_controller_spec.rb module Api::V1 RSpec.describe ScriptsController, type: :controller do let(:user) { FactoryBot.create(:use...
AakLak
1

votes
2

answer
402

Views

Rspec's expect change count not working

Here I'm testing the changes in current_user.messages.count after the current user sends a valid message. Here's my code: spec scenario 'adds to their messages', js: true do expect { find('#message_content').send_keys(:enter) }.to \ change(current_user.messages, :count).by(1) end test.log # ... Conv...
gilbertlcsndle
1

votes
0

answer
58

Views

Capybara Rspec routing incorrectly

I'm working on feature tests for my rails app and completely confused. I've created a test to log in which is routing me back to my root path of /. 1) Sign in user can sign in with valid credentials Failure/Error: expect(page).to have_content I18n.t 'devise.sessions.signed_in' expected to find text...
Godzilla74
1

votes
0

answer
78

Views

rspec returns nil instead of default database value

I have a spec that posts to an endpoint and then checks to see if the default value has been assigned, but it fails (expected: 'uncategorized' got: nil): it 'defaults to uncategorized when none is given' do post wikis_path(params: { wiki: { content: 'Some content' }}) expect(Wiki.last.category).to e...
oneWorkingHeadphone
1

votes
2

answer
128

Views

RSpec expectation on stub fails

Here's the call I would like to test expectations for: UserMailer.invoice_paid(user, invoice).deliver_later Rails 5 ActionMailer behaviour in :test mode seems to return nil on every method of an ApplicationMailer class. This should be fine since I can just stub it like so: invoice_paid_dbl = double(...
Archonic
1

votes
1

answer
70

Views

Checking on empty gems cache fails using Serverspec for Docker image build testing

I'm currently having an issue with testing Docker image builds by means of Serverspec. In a nutshell, what I want to do is ensuring that during the image build the Ruby gems build cache gets cleared explicitly, e.g. by issuing rm -rf /usr/lib/ruby/gems/*/cache/*.gem in the Dockerfile. The Dockerfil...
pklndnst
1

votes
1

answer
121

Views

Cocoon callbacks aren't fired when called from rspec

I have a page with nested fields that works well in development environment. I use an after-insert callback to number the field labels. So this creates the 'pericoop 1, pericoop 2' labels. However, when I test the page from rspec, the labels aren't numbered. When I save_and_open_page, I see just the...
andreheijstek
1

votes
1

answer
161

Views

Rspec: Save record after previous tests

I want to run tests dynamically, using the loop. What I have: let!(:product) { create(:product) } let!(:data) { product.some_data_object } let(:run_action) { patch :next_status, params: { id: data.id } } STATUSES.each_with_index do |status, index| it 'changes status from #{status} to #{STATUSES[inde...
taras
1

votes
0

answer
76

Views

How to print an exception under rspec execution log and rails console?

When Letter Opener email is triggered, it get printed into test.log. I need it to print it under console log. When Feature spec is running and if exception is occurred, it should get printed into server log on terminal. How to inspect the exception when email is triggered in letter_opener_email conf...
Rhunal
1

votes
1

answer
273

Views

rspec issue while testing two classes with the same name under different namespaces (modules)

Disclaimers: I'm pretty new to ruby Please don't ask me why I'm using the same class name in two different modules. I'm only trying to understand why rspec behaves this way; I found it confusing and could not find answers online. Description of issue: I have two .rb files in /lib. Both have a class...
Deba_san
1

votes
0

answer
10

Views

DRY-up `change` matcher expectations

Is there any better way to DRY below expectation? expect{ create(:counter) }.to change(Counter.count).by 1 expect{ create(:counter) }.to change(User.count).by 1 Below worked: [Counter, User].each do |model| expect{ create(:counter) }.to change(model.count).by 1 end Looking for the syntax like below(...
Rhunal
1

votes
1

answer
36

Views

Rails custom model validator fails rspec tests

I have a custom validator to verify the content of 2 fields in my database. When I use it through my UI, it works fine, however my rspec tests are failing and I can't understand why. Here is the rspec test: require 'rails_helper' RSpec.describe Device, type: :model do before(:each) { @user = User.c...
Godzilla74
1

votes
0

answer
20

Views

Rspec tests to voluntarily drop db connection

We have a RoR 5 app which has threads created in its rails runner script. I need to test on how system behaves if the app drops off db connections(ActiveRecord, MYSQL database). This occurred in production and the app went to 'unable to connect to mysql error' state . Manually, I can just reboot the...
user2452057
1

votes
1

answer
285

Views

How to click on the second element in capybara with css

I have a set of following code inside table: I am using capybara and I want to click on the second element. I tried using: page.all(:xpath, '//i[@class='fa fa-pen']')[1].trigger('click') -->resulted in clicking all elements and page.all(:xpath, '//i[@class='fa fa-pen']')[1].click -->resulted in Cl...
A J
1

votes
0

answer
109

Views

ActiveJob Rspec Test failing

I have the following Rspec test for an ActiveJob: require 'rails_helper' RSpec.describe ProofreadDocumentReportJob, type: :job do before do ActiveJob::Base.queue_adapter = :test end subject(:job) { described_class.perform_later(create(:report)) } it 'queues the job' do expect { job } .to have_enque...
chell
1

votes
1

answer
236

Views

How to test ActiveAdmin?

I'm using ActiveAdmin on my rails project, and I would like to know how test it, like how does activeadmin usually test? Since there's no tutorial on the internet, is it necessary to test it?, like this answer I found https://softwareengineering.stackexchange.com/questions/256557/should-i-be-writing...
gilbertlcsndle
1

votes
0

answer
74

Views

RSpec: application layout rendering blank

I am working in a mountable engine, which might have something to do with this problem. I have a page on which I am trying to test for the presence of some content. Right now, I have hardcoded an inline template. pagescontroller#show: def show respond_to do |format| format.html { render inline: 'Du...
Feliks Montez
1

votes
1

answer
75

Views

How to check session params in rspec?

When user visit some page on the site, his session marked with some values. How I can check it with Rspec? I can't find any information about it. Update: Rails 4.2.8
alexeysh
1

votes
2

answer
177

Views

RSpec check title

help me please. I need to test view title with rspec, capybara. I have spec: require 'rails_helper' RSpec.describe 'static_pages/home' do it 'display correct title' do render expect(response).to have_title('Home page') end end home.html.erb: djeje Home pagexbdkek Hello RSpec is passed without failur...
Postanova
1

votes
0

answer
59

Views

rspec/mocha error when testing facter fact

I've written a very simple facter fact. It looks like this: # Kubadmin facts # require 'json' Facter.add(:has_kubectl) do confine :kernel => :linux setcode do Facter::Core::Execution.which('kubectl') end end Facter.add(:kubernetes_version) do confine :kernel => :linux kubernetes_json = Facter::Core:...
jaxxstorm
1

votes
1

answer
136

Views

Ruby on rails: Rspec routing error when testing for a successful http status

So I am having trouble passing these specs in a ruby on rails application: require 'rails/all' RSpec.describe WikisController, type: :controller do let(:user) { User.create!(email: '[email protected]', password: 'password') } let(:wiki) { Wiki.create!(title: 'New Wiki Title', body: 'New Wiki Body',...
Claire Meadows
1

votes
1

answer
75

Views

Error running rspec in window: Webpack binstubs not found

When I run rspec I get the following error: C:\Users\Chloe\workspace\catalyst_research>rspec Randomized with seed 41345 FFFFFFF............................FFFFFFFC:/ruby24/lib/ruby/gems/2.4.0/gems/webpacker-2.0/lib/tasks/installers.rake:1: warning: already initialized constant INSTALLERS C:/ruby24/l...
Chloe
1

votes
0

answer
93

Views

Undefined method index? pundit testing in Rails

I am using Pundit for authorization in my application with rspec for testing. require 'rails_helper' describe SubjectPolicy do subject { described_class.new(user, subject) } let(:subject) { Subject.create } context 'is an administrator' do let(:role) { Role.create(role_name: 'admin') } let(:user) {...
Trenton Tyler
1

votes
0

answer
559

Views

`require': cannot load such file — rspec/core/formatters/base_formatter (LoadError)

I am running rspec and encountered the following error. I already looked for the similar posts but couldn't find the one that is relevant in this case. 'require': cannot load such file -- rspec/core/formatters/base_formatter (LoadError) Ruby version: 2.4.3 Here is my Gemfile: group :test do gem 'dat...
Makoto Taguchi
1

votes
1

answer
138

Views

Suppress STDOUT during RSpec, but not Pry

I'm testing a generator, which outputs a lot of stuff to STDOUT. I want to suppress this, and there are lots of answers for that. But I want to still be able to use pry. Right now, I have to disable the suppression if I need to pry into the test state. I was using this code. It bypassed pry entirely...
Phillip Longman
1

votes
1

answer
116

Views

rspec and friendly_id mismatches

I'm having trouble passing these tests: # user_controller_spec.rb describe 'GET #edit' do user = FactoryBot.create(:user) it 'returns http success' do get :edit, params: {id: user.hash_id} expect(response).to have_http_status(:success) end it 'renders :edit' do get :edit, params: {id: user.hash_id}...
Stephen
1

votes
2

answer
160

Views

Rspec + Capybara - Paypal Express Sandbox checkout timing

My test setup is Rspec + Capybara with Poltergeist / Phantomjs. I'm trying to test the Paypal express sandbox - sometimes the tests pass sometimes not. Looks like a timing problem. Here's my test snippet which redirects to Paypal and on success back to my page. I'm using my credit on the sandbox acc...
Oliver
1

votes
0

answer
203

Views

Stub request when request body is different

Hi I am working on RoR project with ruby-2.5.0 and Rails 5. I am stubbing an http request with stub_request. This http request is basically a mailjet api request to send email. So, I can not just reuse the snippet, rspec spits out to me, because body differs on every execution. Stub request stub_re...
awsm sid
1

votes
2

answer
82

Views

Rails: Better way to write test case for concern

I have following code in concern: module UserStatus extend ActiveSupport::Concern included do enum status: %i[not_started in_progress completed] end end I am trying to write an rspec test for it. require 'rails_helper' RSpec.describe 'user_status' do it 'test' do byebug should define_enum_for(:statu...
User7354632781
1

votes
0

answer
119

Views

Unable to find field “username” that is not disabled (Capybara::ElementNotFound)

I'm trying to test a simple login form using cucumber / rspec / capybara. But I'm having trouble detecting elements on the page and doing any sort of test. Unable to find field 'username' that is not disabled (Capybara::ElementNotFound) Feature File Feature: Login Scenario: correct login credentia...
Thomas O'keeffe
1

votes
1

answer
58

Views

rspec block return value

I have an issue that is extremely similar to a previously posted RSpec and Faraday question. To keep things clear, I'm going to borrow that other question's code and make a simple modification that is causing me much sorrow. class Gist def self.create(options) post_response = Faraday.post do |reques...
Chuck Remes

View additional questions