Rspec 3 how to test flash messages
You are testing for the presence of flash[:success]
, but in your controller you are using flash[:notice]
Rails 5.1 syntax to test flash message in a system test
I ended up changing the assertion from:
assert_equal 'User was successfully created.', flash[:notice]
to:
assert_selector "#notice", text: 'User was successfully created.'
which seemed to do the trick.
Rspec testing of flash messages
In your controller, you're checking whether the URL is an empty string:
if params[:user][:jira_url] == ""
But in your spec, you're sending a POST like this:
post :check_jira_url, {:jira_url => ''}
The params in your controller look something like {:jira_url => ''}
rather than {:user => {:jira_url => ''}}
which your controller would expect.
So the line in your spec should look like:
post :check_jira_url, {:user => {:jira_url => ''}}
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
How to test flash component with rspec
Finally, i've fixed it adding controller.
before flash:
with_controller_class Customer::DashboardController do
controller.flash[:notice] = "Notification message!"
render_inline described_class.new()
end
How to stub a flash in a RSpec spec?
Your spec is a feature spec, so the spec environment has no access to things like the flash. Don't try to work with the flash directly. Instead, ideally, test that the user's view of the app looks and/or behaves the way that it should if the flash value is set the way that it should be. I wouldn't just test that the hidden field is present in the form; I'd test that it has the effect that it should after the form is submitted. That's what feature specs are all about: testing that the application works as a whole from the user point of view.
If the flash value isn't ever used in the UI, just logged or stored in the database, it would be OK to test that the log line or model object has the value that's stored in the flash. (The user here is the admin who would look at the log or whatever later.) But if the flash does affect the UI, testing that is preferable.
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
Rails with Paypal Permissions and Paypal Express Checkout
Invalid Multibyte Char (Utf-8) Error, Ruby
Could Not Find a Valid Gem 'Rails' (>= 0) in Any Repository
How to Call Java API from Ruby 1.8 or 1.9
Best Practice to Mark Deprecated Code in Ruby
Capistrano Asks for Password When Deploying, Despite Ssh Keys
How to Fetch Linkedin User Data
Ruby Shoes Gui: Continually Updating Paragraphs
Why Does This Nokogiri Xpath Have a Null Return
Ruby Array to String Conversion
Link_To Method and Click Event in Rails
Ruby: How to Load a File into Interactive Ruby Console (Irb)
Having Trouble Installing Any Ruby 1.9.X (With Rbenv) on MAC Osx Due to Psych Yaml Parse Errors
Sidekiq Configuration for Multiple Environments
How to Ban/Block Users with Devise for Rails
M Hartl's Ruby on Rails Tutorial Chapter 5 Custom Title on Home Page