Questions tagged [shoulda-matchers]
13 questions
3
votes
1 answer
rspec shoulda matchers is expected to belong to polymorphic
I am trying to test my polymorphic association but I cant seem to get it to pass
Model:
class Foo < ApplicationRecord
belongs_to :bar, polymorphic: true, optional: true
end
Now my test looks like
RSpec.describe Foo, type: :model do
subject {…

MZaragoza
- 10,108
- 9
- 71
- 116
3
votes
2 answers
ShouldaMatchers validate_uniquess_of scoped to multiple columns
I have a model with a unique index at the db level and model validation checking for uniqueness scoped to multiple columns.
# db level
add_index :table1, %i[fk_belongs_to_id col1 col2 col3 col4], unique: true
# model level
validates…

Int'l Man Of Coding Mystery
- 1,632
- 2
- 14
- 29
2
votes
1 answer
Shoulda::Matchers::Independent::DelegateMethodMatcher: undefined method `allow_nil'
I'm trying to test a delegate method of my Comment model using shoulda-matchers (3.1.2) & rspec-rails (3.7.2) in my rails (5.0.1) application with ruby 2.5.0-p0 (x86_64-linux)
and getting this error:
1) Comment delegation
Failure/Error: it {…

You Nguyen
- 9,961
- 4
- 26
- 52
0
votes
2 answers
validation_inclusion_of rspec test case is returning ArgumentError
Let's consider a class with definition as shared below:
# a_type :string
class Foo < ActiveRecord::Base
enum a_type: { fir: 'first', sec: 'second' }
validates :a_type, presence: true, inclusion: { in: a_types.keys }
end
Rspec:
describe…

piyush
- 1
- 2
0
votes
1 answer
Testing requests with rspec
hey guys, am trying to build tests for my controllers using rspec , I have 3 models and controllers i.e user, payment and home
-These mode;s are associated in search a way that, payment belongs to user and home, home has many payments, and user has…

mutebi ug official
- 15
- 8
0
votes
2 answers
Rails test using rspec, shoulda_matcher and factory_bot_rails rturns PG::NotNullViolation: ERROR: null value in column
I have 2 model: Answer and Book.
Although they are very similar, all tests pass to Answer but 1 test fails for Book
models/answer.rb
class Answer < ApplicationRecord
belongs_to :question, class_name: "Question", foreign_key: "question_id"
…

Wagner Braga
- 459
- 4
- 9
0
votes
1 answer
Why are tests failing due to validation?
I am writing tests for an application and ran into a problem.
I have the next validation im my model:
def correct_date
errors.add(:occured_at, 'must be in past or present time') if occured_at > 5.minutes.from_now
end
And when I run even the…

SsPay
- 177
- 1
- 10
0
votes
0 answers
shoulda-matchers are not matching
I am creating a TextMessage model to manage communications.
I am using shoulda-matchers' for testing
my model
class TextMessage < ApplicationRecord
validates :message, presence: true
end
Now my test
RSpec.describe TextMessage, type: :model do
…

MZaragoza
- 10,108
- 9
- 71
- 116
0
votes
2 answers
Shoulda matchers - Rspec - Test controllers Actions to Json Response - FastJSON
How to test using shoulda-matchers for these actions, to response JSON success 200, and fail 422
and how to test with normal RSpec: both cases if you can:
I think there is missing some information for shoulda matches on its repository.
if you can…

Gonza
- 91
- 1
- 7
0
votes
1 answer
NoMethodError: undefined method `belong_to' for test when using shoulda-matchers in unit test
I tried to use shoulda-matchers to test the association between models. However, it always show Error:
TakingTest#test_belongs_to:
NoMethodError: undefined method belong_to' for #
…

reedandchrissy
- 3
- 1
0
votes
0 answers
Rspec testing strong params in Rails not working (wrong number of arguments)
I'm trying to implement some testing for my controller strong params and following the docs to the 'T'.
My controller:
class FutureOutagesController < ApplicationController
before_action :find_future_outage, only: [:show, :update, :destroy]
…

Demian Sims
- 871
- 1
- 14
- 29
0
votes
1 answer
I need to write specs to check permitted params for create action and getting error
I need to write specs to check permitted params for create action.
module Backoffice
class JobsController < BaseController
def create
@job = Job.new(job_params)
end
def job_params
params.require(:job).permit(:title)
…

Alex Kleshchevnikov
- 336
- 6
- 20
-1
votes
1 answer
For an ActiveRecord model with overridden `New` method, how to provide custom test subject with parameters using FactoryGirl in Rails shoulda matchers
I have an Rails ActiveRecord model which has the new method overridden. The new method accepts a parameter and instantiates an object of this model, with other required fields.
In RSpec Model specs with Shoulda matchers, I'm testing…

Anas Shahid
- 3
- 1