Module: RSpec::Rails::Matchers
- Included in:
- RailsExampleGroup
- Defined in:
- lib/rspec/rails/matchers/have_http_status.rb,
lib/rspec/rails/matchers.rb,
lib/rspec/rails/matchers/be_a_new.rb,
lib/rspec/rails/matchers/be_valid.rb,
lib/rspec/rails/matchers/redirect_to.rb,
lib/rspec/rails/matchers/be_new_record.rb,
lib/rspec/rails/matchers/have_rendered.rb,
lib/rspec/rails/matchers/routing_matchers.rb
Overview
The following code inspired and modified from Rails' assert_response
:
Thank you to all the Rails devs who did the heavy lifting on this!
Defined Under Namespace
Modules: HaveHttpStatus, RedirectTo, RenderTemplate, RoutingMatchers Classes: BeANew
Instance Method Summary collapse
-
#be_a_new(model_class) ⇒ Object
Passes if actual is an instance of
model_class
and returnsfalse
forpersisted?
. -
#be_new_record ⇒ Object
Passes if actual returns
false
forpersisted?
. -
#be_valid(*args) ⇒ Object
Passes if the given model instance's
valid?
method is true, meaning all of theActiveModel::Validations
passed and no errors exist. -
#have_http_status(target) ⇒ Object
Passes if
response
has a matching HTTP status code.
Instance Method Details
#be_a_new(model_class) ⇒ Object
Passes if actual is an instance of model_class
and returns false
for
persisted?
. Typically used to specify instance variables assigned to
views by controller actions
Use the with
method to specify the specific attributes to match on the
new record.
74 75 76 |
# File 'lib/rspec/rails/matchers/be_a_new.rb', line 74 def be_a_new(model_class) BeANew.new(model_class) end |
#be_new_record ⇒ Object
Passes if actual returns false
for persisted?
.
22 23 24 |
# File 'lib/rspec/rails/matchers/be_new_record.rb', line 22 def be_new_record BeANewRecord.new end |
#be_valid(*args) ⇒ Object
Passes if the given model instance's valid?
method is true, meaning all
of the ActiveModel::Validations
passed and no errors exist. If a message
is not given, a default message is shown listing each error.
41 42 43 |
# File 'lib/rspec/rails/matchers/be_valid.rb', line 41 def be_valid(*args) BeValid.new(*args) end |
#have_http_status(target) ⇒ Object
Passes if response
has a matching HTTP status code.
The following symbolic status codes are allowed:
Rack::Utils::SYMBOL_TO_STATUS_CODE
- One of the defined
ActionDispatch::TestResponse
aliases::error
:missing
:redirect
:success
351 352 353 354 |
# File 'lib/rspec/rails/matchers/have_http_status.rb', line 351 def have_http_status(target) raise ArgumentError, "Invalid HTTP status: nil" unless target HaveHttpStatus.matcher_for_status(target) end |