How to Attach a Message to Rspec Check

How to attach a message to RSpec check?

For RSpec 3+:

The message could be customized as a string or using a proc(check the reference).

expect(1).to eq(2), 'one is not two!'

Customized message
RSpec tries to provide useful failure messages, but for cases in which you want more
specific information, you can define your own message right in the example. This works for
any matcher other than the operator matchers.

source @ relishapp


For older RSpec versions

should and should_not take a second argument (message) that overrides the matcher’s default message.

1.should be(2), 'one is not two!'

The default messages are usually pretty useful though.

How to write a test case using rspec for a notice message

Use a feature spec (an integration test) instead of a controller spec to test the application as seen by the user:

# spec/features/topics.rb
require 'rails_helper'
RSpec.feature "Topics" do
scenario "when I create a topic with valid attributes" do
visit '/topics/new'
fill_in 'Topicname', with: 'Behavior Driven Development' # Adjust this after whatever the label reads
click_button 'create topic'
expect(page).to have_content 'Topic was created successfully!'
end

scenario "when I create a topic but the attributes are invalid" do
visit '/topics/new'
fill_in 'Topicname', with: ''
click_button 'create topic'
expect(page).to_not have_content 'Topic was created successfully!'
expect(page).to have_content "Topicname can’t be blank"
end
end

While you can poke around the flash hash you should have an integration test covering this anyways since controller tests are flawed and will not cover for example errors in the routes since large portions of the application are stubbed out.

In fact you may want to reconsider using controller specs at all since both the RSpec and Rails teams suggest using integration testing instead. If you want to test at a lower level than a feature spec use request specs.

See:

  • Replacing RSpec controller
    specs
  • Deprecate ActionController::TestCase in favor of ActionDispatch::IntegrationTest

Rspec doesnt show the content in mail body

expect(mail.body).to include("Welcome user") will only actually work if you're sending a simple email with only text. When you're sending a multipart email you need to match the specific part of the email:

let(:html_body) do
mail.body.parts.find { |p| p.content_type.match 'text/html' }.body.raw_source
end

it "contains hello world" do
expect(html_body).to include("Welcome user")
end

How to insert an attribute into rspec test for validation messages using i18m

I found the answer. I needed to pass human_attribute_name of the attribute into the translation as a parameter attribute.

The test now passes like this:

  it "is invalid with special characters" do
account = FactoryGirl.build(:account, name: "test_account_*namre")
account.valid?
expect(account.errors[:name]).to include(I18n.t('activerecord.errors.models.account.attributes.name.invalid', attribute: Account.human_attribute_name(:name)))
end

Rails - How to test that ActionMailer sent a specific attachment?

Here's an example that I copied from my rspec test of a specific attachment, hope that it helps (mail can be creating by calling your mailer method or peeking at the deliveries array after calling .deliver):

  mail.attachments.should have(1).attachment
attachment = mail.attachments[0]
attachment.should be_a_kind_of(Mail::Part)
attachment.content_type.should be_start_with('application/ics;')
attachment.filename.should == 'event.ics'

How to test if a class receives a message in RSpec?

You need to set up the mock before you call the method. RSpec provides two ways to do this:

  1. Move the expectation before service.call:

    describe "#find_contract" do
    it "returns contract based on contract type." do
    service = SecurityQueryService.new(user,
    type: 'equity',
    ticker_name: equity_contract.ticker
    )
    expect(EquityContract).to receive(:find_by)
    service.call
    end
    end
  2. Set up the method as a spy by allowing the method to be called, then expect it to have been called after the fact:

    describe "#find_contract" do
    it "returns contract based on contract type." do
    service = SecurityQueryService.new(user,
    type: 'equity',
    ticker_name: equity_contract.ticker
    )
    allow(EquityContract).to receive(:find_by)
    service.call
    expect(EquityContract).to have_received(:find_by)
    end
    end

As you can see, the first method requires the least typing, but requires awkward thinking ahead. The second method is more verbose, but is more logical in that it puts the expectation after the method call.

RSpec - how to test if object sends messages to self in #initialize

Spies are an alternate type of test double that support this pattern
by allowing you to expect that a message has been received after the
fact, using have_received.

https://relishapp.com/rspec/rspec-mocks/v/3-5/docs/basics/spies

Only spy object can store the method calls. To test your real class in the way that you want, you have to use expect_any_instance_of statement before the class will be initialized:

expect_any_instance_of(Picture).to receive(:add_single_pics)
Picture.new('Test Picture', 'Test-Picture')

In this case your add_single_pics method will be called, but its logic will not be run, if you need to run it you need to call the and_call_original method on the matcher:

expect_any_instance_of(Picture).to receive(:add_single_pics).and_call_original


Related Topics



Leave a reply



Submit