module RSpec::Rails::Matchers
Container module for Rails specific matchers.
Public Instance Methods
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.
@example
get :new assigns(:thing).should be_a_new(Thing) post :create, :thing => { :name => "Illegal Value" } assigns(:thing).should be_a_new(Thing).with(:name => nil)
# File lib/rspec/rails/matchers/be_a_new.rb, line 76 def be_a_new(model_class) BeANew.new(model_class) end
Passes if actual returns `false` for `persisted?`.
@example
get :new expect(assigns(:thing)).to be_new_record
# File lib/rspec/rails/matchers/be_new_record.rb, line 24 def be_new_record BeANewRecord.new end
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.
@example
thing = Thing.new expect(thing).to be_valid
# File lib/rspec/rails/matchers/be_valid.rb, line 43 def be_valid(*args) BeValid.new(*args) end
@api public 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`
-
@example Accepts numeric and symbol statuses
expect(response).to have_http_status(404) expect(response).to have_http_status(:created) expect(response).to have_http_status(:success) expect(response).to have_http_status(:error) expect(response).to have_http_status(:missing) expect(response).to have_http_status(:redirect)
@example Works with standard `response` objects and Capybara's `page`
expect(response).to have_http_status(404) expect(page).to have_http_status(:created)
@see github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse` @see github.com/rack/rack/blob/master/lib/rack/utils.rb `Rack::Utils::SYMBOL_TO_STATUS_CODE`
# File lib/rspec/rails/matchers/have_http_status.rb, line 355 def have_http_status(target) raise ArgumentError, "Invalid HTTP status: nil" unless target HaveHttpStatus.matcher_for_status(target) end