Skip to main content

All Questions

Filter by
Sorted by
Tagged with
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 &...
Backo's user avatar
  • 18.8k
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, :...
Backo's user avatar
  • 18.8k
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 &...
hazg's user avatar
  • 348
2 votes
0 answers
303 views

Testing with RSpec that a method uses the right DB replica

I want to setup a test that checks for a given method that that method will use the right replica role (using the Multiple Database feature by ActiveSupport since Rails 6.1) Let's pretend that we have ...
Andrea Salicetti's user avatar
1 vote
3 answers
641 views

Why is this attibute on a subject not being set in a rails rspec model spec?

I have a spec testing a model that looks like this: RSpec.describe SomeModel, type: :model do subject { described_class.new(test_amount: 99) } describe 'validates a column' do it 'does some ...
CafeHey's user avatar
  • 5,800
1 vote
1 answer
550 views

How to pass a variable in callbacks during rspec testing?

I have callback name authenticatie_admin_user . It will return true if current user email ID is "[email protected]" def authenticate_admin_user! if Current.user.blank? || Current.user....
Ranjan Ghate's user avatar
0 votes
0 answers
128 views

Inside an RSpec matcher, how to make an instance available that gets created inside a model callback

I try to test a model. After the model under test is created, it creates an object from another model and sends out an email based on the other object's ID. # model under test class ...
Rich Steinmetz's user avatar
0 votes
1 answer
471 views

Rspec raise matcher not working, copied syntax from docs?

rspec-core (3.9.1) rspec-expectations (3.9.0) rspec-mocks (3.9.1) rspec-rails (4.0.0.beta4, 3.9.0) rspec-support (3.9.2) According to docs: https://relishapp.com/rspec/rspec-expectations/v/3-9/docs/...
james's user avatar
  • 4,029
2 votes
1 answer
3k views

How can you run a single RSpec example many times in parallel for deflaking?

I have an RSpec test that is flaky. To reproduce the failure I want to run the test hundreds of times as fast as I can. How can I run a single example many times in parallel? Is there a better way to ...
Alan Savage's user avatar
0 votes
1 answer
2k views

Rails 6 (edge) 'test_helper', 'rails_helper', 'spec_helper' - what's the difference?

I am running a Rails 6 application (edge branch) and by default it comes with a test_helper.rb file which all the empty tests require by default: require 'test_helper' class UserTest < ...
JoshuaESummers's user avatar
0 votes
1 answer
195 views

Why is Application Record changing my RSpec Test result

Reasonably green to testing but I was following along with a simple udemy course. I used the RSpec documentation to set up RSpec in rails to try out some testing. But I have come across an issue that ...
Tigiras's user avatar
  • 57
1 vote
2 answers
965 views

Passing bearer token in RSPEC (no implicit conversion of nil into String)

require 'rails_helper' RSpec.describe "User management", :type => :request do describe "Example::V2::Users" do describe "GET /api/v2/users/" do it 'returns status 200, ...
David Sarvasidze's user avatar
2 votes
1 answer
103 views

How to write a test for validates_uniqueness_of with scope and presence: true RSpec Rails

Trying to write a test to verify uniqueness of :user_id with scope :board_id. Model: class Membership < ApplicationRecord belongs_to :board belongs_to :user validates_uniqueness_of :user_id,...
Roman's user avatar
  • 763
0 votes
1 answer
253 views

How to test json format with specific url use RSpec Rails 4.0?

I need to test url like "/users.json?search=" + "[params]": it "renders a successful response" do get :show, params: "/users.json?search=c" expect(response).to have_http_status(200) expect(...
Roman's user avatar
  • 763
0 votes
0 answers
259 views

Writing RSPECs / Failed tests. What is wrong with my rspec files?

I am trying to write specs for my Pinterest clone app. My app is working as intended but is failing my tests. What am I not understanding? pin_controller_spec require 'spec_helper' require 'rspec-...
Dustin Longenecker's user avatar
1 vote
1 answer
217 views

errors.added? returns false when used with :exclusion

In a recursive rails model I have a validation that the model cannot reference itself: validates :parent_entity, exclusion: { in: ->(entity) { [entity] } } This is successful and the exclusion ...
alexanderdavide's user avatar
1 vote
1 answer
840 views

Using "file_fixture" in before(:suite)?

I want to do this in the spec_helper: config.before(:suite) do allow(Net::HTTP) .to receive(:get) .with(URI(some_uri)) .and_return(file_fixture('response.json').read) end But ...
zarathustra's user avatar
  • 2,060
1 vote
1 answer
647 views

How do I write a request RSpec for a request that should fail behind Pundit's authorization mechanism?

I set up Pundit to guard a bunch of request paths, and it's working fine. In particular, if I hit /api/users/:id with a PATCH request, passing the relevant parameters, I get a 403 if I'm not ...
Morpheu5's user avatar
  • 2,760
0 votes
0 answers
128 views

rspec expectations change depending on other expectations existing

I have a rails app tested with rspec using factorybot. Users have many entries. Entries belong to a category. Users have many categories through entries. I created a user and put an expectation in to ...
Travis's user avatar
  • 518
0 votes
1 answer
838 views

RSpec: How to properly test class method that loops through a collection / calls instance method

Consider the following class and class method: class Foo < ActiveRecord::Base scope :active, -> { where(deleted: false) } class << self def some_class_method active....
peterlawless's user avatar
3 votes
2 answers
10k views

test that method was called inside another method with rspec

I have to write a spec for the following method: def track_event tracker = set_tracker category = define_event_category tracker.event(category: category, action: name, label: properties['...
user avatar
0 votes
1 answer
520 views

Rspec: how to create a method to call and create all mocks?

I am using Rspec to test my rails application and FactoryBot (new nome for factory girl) to create mocks. But in each file I need to call a lot of mocks. And it's almost the something, call some mocks,...
Lucas Andrade's user avatar
1 vote
2 answers
1k views

Rspec how to create an method to "DRY" only some params of a request?

I want to test a create method of my project, but this create method has 3 steps in my form and I want to test all of them. To test each step I need to send a create request with their respective ...
Lucas Andrade's user avatar
1 vote
1 answer
1k 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 ...
gbertl's user avatar
  • 351
0 votes
0 answers
68 views

How to convert database calls with mocks/doubles, in Rails and Rspec?

I am working on a reminder scheduler and used rails scaffold to create the models and views for the reminder. So far I have the controller looks like this: class RemindersController < ...
ezio's user avatar
  • 191
2 votes
0 answers
229 views

Rspec concatenate expect statements

expect(a).not_to be(6).or expect(a).to eq(6).and expect(b).to eq(3) This code is not executable. But what I'm trying to test is this statement: (a == 6 AND b == 3) OR a != 6 How do I concatenate ...
Mattia's user avatar
  • 179
0 votes
1 answer
215 views

ArgumentError when trying to pass nested parameters in rspec

When I'm trying to execute the following line post :create, user: Fabricate.attributes_for(:user) I get this error message ArgumentError: unknown keyword: user. Here's the code I was originally ...
David B.'s user avatar
  • 838
3 votes
1 answer
4k views

How to write tests for active record scopes?

How can I write tests for active record scopes? for example class Post < ActiveRecord::Base scope :recent, -> { order("posts.created_at DESC") } scope :published, -> { where("status = 1")...
ltdev's user avatar
  • 4,457
0 votes
2 answers
38 views

Should I Explicitly Spec my Helpers in Rails

While rspec automatically creates specs for any helpers created by the Rails generators, I was wondering if other Rails developers find it important/useful to spec the helpers in real-world or if they ...
Ash's user avatar
  • 25.6k
1 vote
2 answers
1k views

Issues with spec files load order: in `find_and_eval_shared': Could not find shared examples

I'm currently working through Rails 4 Test Prescriptions and have run into an issue with the load order of my spec files that isn't addressed in the book. And unfortunately, not something I can solve ...
jamesemcc's user avatar
  • 139
0 votes
1 answer
83 views

Devise sign up directing to wrong path

I'm trying to set up a simple online store. I'm using the gem devise for user registration. When I run the site on local host everything works fine, however when I run rspec it appears that after ...
Richard Watkins's user avatar
0 votes
1 answer
564 views

Rspec 3 - Testing assign in controller action

Currently I have the following custom action in my controller: def set_active current_user.active_meal_plan = @meal_plan current_user.save respond_with @meal_plan, location: -> { ...
Andres's user avatar
  • 75
0 votes
1 answer
556 views

FactoryGirl How create a instance which is created by association on callback

Consider two models: class User < ActiveRecord::Base has_one :book after_create :create_book end class Book < ActiveRecord::Base belongs_to :user validate_uniqueness :user_id end ...
larryzhao's user avatar
  • 3,203
1 vote
1 answer
1k views

RSpec failure ( undefined method `id' for nil:NilClass ) with has_many through model

I can't make the spec to pass in the relationships controller. I have to change something about my object in the controller or the controller_spec. Feel free to ask any doubt about my controller... ...
Jony's user avatar
  • 157
0 votes
1 answer
323 views

Rspec delete object failure with method has_many throw, No route matches {:action=>"destroy"

Need some help to make rspec test to pass, my goal is to delete a relationship that has been created with the method has_many throw. I followed this MHartl's tutorial. relationships_controller.rb : ...
Jony's user avatar
  • 157
0 votes
2 answers
1k views

Rails failure to load shoulda-matchers 3.1.1 gem, undefined method `allow_value' for #<RSpec

'shoulda-matchers' gem is not loading when running rspec tests. Error: NoMethodError: undefined method 'allow_value' for #<RSpec::ExampleGroups::Contact::ActiveModelValidationss:0x007fef1021b310&...
alisontague's user avatar
0 votes
1 answer
566 views

rspec controller spec for js request

In my controller specs everything works fine (update action, edit action, etc. also via js request), except the create action. For some reason it doesn't change the Task.count, but the http response ...
Sean Magyar's user avatar
  • 2,380
1 vote
2 answers
3k views

rspec controller testing: "expected #count to have changed by -1, but was changed by 0"

am having trouble resolving this error. I'm not sure which part of the controller_spec is written wrongly. Please help! Routes Rails.application.routes.draw do resources :cases, only: [:index, :...
Kate Lim's user avatar
1 vote
1 answer
833 views

Rspec Helper Method Not Accessible to Features

I'm learning TDD and running some Feature tests and am having issues with a helper method. I've pulled out the following method from the feature test: todo_helper.rb module Features def ...
fancy_daniel's user avatar
0 votes
1 answer
2k views

how do you test csv file content in Rails?

I'm writing a simple CanadaPost price shipping API, where the prices are given in a PDF document in table format (see page 3 of https://www.canadapost.ca/tools/pg/prices/SBParcels-e.pdf). The data ...
Jenna S's user avatar
  • 726
0 votes
1 answer
2k views

rspec model spec; testing if local var is assigned

I'd like to test in my model spec if the local var is set in the method. I tried to use assigns, but it doesn't work in model specs. What is the rails way to do this properly? user.rb def ...
Sean Magyar's user avatar
  • 2,380
0 votes
1 answer
218 views

Why are instance variables sometimes overused in RSpec tests? [closed]

I've noticed examples of instance variable usage in RSpec unit tests etc. Something like this: it 'should update something' do @user = user(:userone) @attr = { :name => 'this', :phone ...
wired00's user avatar
  • 14.4k
4 votes
0 answers
2k views

What are the differences between the commands: spec, rspec, rake spec?

Here is a thread I looked at: How to run a single RSpec test? One answer says to use rspec to run tests. Another answer (originally?) said to use rake spec to run tests but then a commenter said it's ...
7stud's user avatar
  • 48.5k
-1 votes
1 answer
503 views

How to test links with RSPEC

I have a side navigation menu and top navigation menu rendering using partials on all pages. On the welcome page, I am trying to check the clicks. I have this, but it is not working at the moment. ...
Tim R's user avatar
  • 524
1 vote
1 answer
3k views

Rspec view test with url parameters

I have a page dashboard.html.erb that may be redirected to from several different controllers/actions. When it receives the redirect, it also receives several url parameters. Controller code: class ...
james's user avatar
  • 4,029
-1 votes
1 answer
99 views

how to check rails full app using rspec test

Factories data also define My Code in controller_spec.rb describe "POST create" do context "with valid attributes" do it "creates a new corporate" do post :create, corporate: FactoryGirl....
Sumit.Daksh's user avatar
9 votes
1 answer
7k views

`method_missing': `build` is not available on an example group (e.g. a `describe` or `context` block)

I wanted to remove the FactoryGirl.build(:user) everytime I want to create a user and so I added these lines: RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end to the ...
Xeen's user avatar
  • 7,003
0 votes
1 answer
56 views

Query after POST returns Nil unless following expect(page)

After I submit a POST, I check the DB to see if it is stored. feature 'foo' do scenario 'bar', :js => true do navigate_to_form within '.form-class' do fill_form click_on '...
omgmakeme's user avatar
  • 766
5 votes
1 answer
2k views

How to set the locale in RSpec View Examples

I want to test the view to make sure that error messages are properly rendered. My config.default_locale is 'fr'. So, I expect my view to find the proper Active Record error message from my french ...
Gilles's user avatar
  • 167
102 votes
2 answers
32k views

How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

I am doing the Rails Tutorial for the second time. When I enter this rails generate integration_test static_pages I get spec/rails_helper.rb and spec/spec_helper.rb instead of just spec/spec_helper....
user3417583's user avatar
  • 1,437