Customize Minitest output to growl - ruby-on-rails

In my rails app I am using:
guard
guard-minitest
growl
when a test runs in the background:
I get this from Growl:
Great to know that at least one test failed. Any way to add that summary that reports the number i.e. 2 failures etc.?

You can change the behavior by modifying Guard::Minitest::Notifier.notify:
require 'guard/compat/plugin'
module Guard
class Minitest < Plugin
class Notifier
# ...
def self.notify(test_count, assertion_count, failure_count, error_count, skip_count, duration)
message = guard_message(test_count, assertion_count, failure_count, error_count, skip_count, duration)
image = guard_image(failure_count + error_count, skip_count)
# title: was just 'Minitest results'
Compat::UI.notify(message, title: message, image: image)
end
end
end
end
The title 'Minitest results' is most likely just a cludged placeholder. This example sets it to the same output as you get in the CLI but you can really do whatever you want here.
You would preferably do this by forking the gem and setting your gemfile to pull from your fork.
gem 'minitest-guard', github: 'yourusername/minitest-guard'

Related

Capybara tests don't work, while actual app works fine. React front-end in Rails 5

Specs: Rails 5.2, Ruby 2.5.1, Yarn 1.91.1, React-Rails 2.6.0, selenium-webdriver 3.14, chromedriver 77, capybara gem 3.29 , Ubuntu 18.04
Issue: I created a questionnaire using react components in a Rails app which works when run in development mode. I can click the yes or no button then a callback function renders the new question text. However my selenium integration tests don't pick this up. The page object continues to have the same text for the first question so I receive an error like this
Failure/Error: expect(question.text).to include('last question')
expected "first question" to include "last question"
The test itself looks like this in features/questionnaire.rb
RSpec.feature "Onboarding Questionnaire", type: :feature, js:true do
(...)
it...
question = find('h3.question')
expect(question.text).to include('first question')
yes = find('#yes')
yes.click
sleep 5
question = find('h3.question')
expect(question.text).to include('last question')
The problem arises after yes.click. I thought at first my animations were causing the issue, but I removed those and it is just using setState in my callback function.
Here is my react callback function
saveAnswer(e) {
questionnaire = this
++questionnaire.state.page
questionnaire.setState({page: questionnaire.state.page})
}
Here is my driver config
Capybara.javascript_driver = :selenium_chrome_headless
Now using sleep works as desired. Here are the changes I made.
1) I removed the deprecated gem chromedriver-helper and replaced it with webdrivers
2) Added require 'webdrivers' to the rails_helper.rb file.
3) Found error in my react code (which wasn't showing up in development or being logged in chrome webdriver) of questionnaire = this which I changed to var questionnaire = this.
Integration tests now pass fine using selenium_chrome_headless

Publish a Sidekiq schedule job through Private Pub in rails

I am using 'Sidekiq' to schedule reminder about any task on given time. Its working perfect. Now i want to append it to notify on my navbar, for it i am using 'Private Pub' to publish the reminder message.
Here is code of Sidekiq Worker.
class ReminderWorker
include Sidekiq::Worker
def perform(args)
reminder = Reminder.find(args['id'])
reminder.activate = true
PrivatePub.publish_to("reminder", message: reminder)
reminder.save
end
end
and inside "application.js" i am using alert for testing but it is not working.
PrivatePub.subscribe("reminder", function(data, channel) {
return alert('Remarks ='+ data.message.remarks);
});
Am i missing some thing? As it is possible to publish data through Private Pub in rb file accourding to Ryan http://railscasts.com/episodes/316-private-pub?autoplay=true
This gem has been updated last time three years ago and seems not maintained.
Maybe it's better to use newest rails solution like ActionCabble? https://github.com/rails/rails/tree/master/actioncable
I found this issue, that proofs that ActionCabble can be integrated to ActiveJob or other background workers:
https://github.com/rails/rails/issues/22897

ActionCable channel not accessible outside rails plugin

I'm trying to create a gem which exposes an ActionCable channel, but I can't get it to work.
This is my gem
# lib/my_channel.rb
class MyChannel < ActionCable::Channel::Base
def wait(data)
# logic ...
end
end
# lib/engine.rb
module MyApp
class Engine < ::Rails::Engine
isolate_namespace MyApp
end
end
I then add the gem to my main applications Gemfile, run bundle install, start up the console and run MyChannel. Which don't yield and error, meaning that the channel as been included properly.
I then add this to my main application
// application.js
var socket = "ws://localhost:3000/cable";
var cable = ActionCable.createConsumer(socket);
cable.subscriptions.create({ "channel": "MyChannel" }, {
received: function(){
// ...
}
});
But I'm getting this error
Subscription class not found ({"command"=>"subscribe", "identifier"=>"{\"channel\":\"MyChannel\"}"})
What am I missing?
This answer doesn't apply to gems, only making channels within a Rails app.
Your my_channel.rb is located in the wrong place. I'm not sure if behaviour has changed between the betas (I'm using beta3), but it should be located in app/channels/application_cable/.
I was experiencing the same issue, and realized my channel file was named inappropriately (host.rb instead of host_channel.rb). After renaming the file, it started working.
I believe the channel files are only searched for with the specific location and naming scheme (app/channels/application_cable/*_channel.rb) by default.

Configuration of ContentfulModel gem is lost after initialization

Completely new Rails 4.2.3 application. The only changes to the Gemfile have been removal of Spring, addition of dotenv, and the latest contentful_rails and contentful_model gems as published on rubygems.org.
For unknown reasons, the configuration details defined in the initializer are gone by the time the app comes up. It's the same object (same value for ContentfulModel.configuration.object_id) but the values that were previously correct are now nil.
I added an initializer as shown in the README.
$ cat config/initializers/contentful_model.rb
ContentfulModel.configure do |config|
byebug
config.access_token = ENV['CONTENTFUL_ACCESS_TOKEN']
config.preview_access_token = ENV['CONTENTFUL_PREVIEW_ACCESS_TOKEN']
config.space = ENV['CONTENTFUL_SPACE']
# config.options = {
#extra options to send to the Contentful::Client
# }
end
And I defined one model, Category.
$ cat app/models/category.rb
class Category < ContentfulModel::Base
self.content_type_id = "[category content type string]"
end
So here's what happens when I fire up the Rails console:
$ rails c
[1, 9] in /home/trevor/code/chef/www-contentful-rails/config/initializers/contentful_model.rb
1: ContentfulModel.configure do |config|
2: config.access_token = ENV['CONTENTFUL_ACCESS_TOKEN']
3: config.preview_access_token = ENV['CONTENTFUL_PREVIEW_ACCESS_TOKEN']
4: config.space = ENV['CONTENTFUL_SPACE']
5: # config.options = {
6: #extra options to send to the Contentful::Client
7: # }
8: byebug
=> 9: end
(byebug) ContentfulModel.configuration
#<ContentfulModel::Configuration:0x00000005bc7be0 #access_token="[my actual token string]", #entry_mapping={}, #preview_access_token="[my actual preview token string]", #space="[my actual space]">
(byebug) continue
/home/trevor/.rvm/gems/ruby-2.2.2#www-contentful-rails/gems/actionpack-4.2.3/lib/action_dispatch/http/mime_type.rb:163: warning: already initialized constant Mime::JSON
/home/trevor/.rvm/gems/ruby-2.2.2#www-contentful-rails/gems/actionpack-4.2.3/lib/action_dispatch/http/mime_type.rb:163: warning: previous definition of JSON was here
Loading development environment (Rails 4.2.3)
2.2.2 :001 > ContentfulModel.configuration
=> #<ContentfulModel::Configuration:0x00000005bc7be0 #access_token=nil, #entry_mapping={"[category content type string]"=>Category}, #preview_access_token=nil, #space=nil>
2.2.2 :002 >
I've spent a bunch of time sifting the gem source and stepping through the debugger without results. I've posted an issue for the project on GitHub because I haven't been able to identify the source of the problem and I have to assume its within the gem. Any assistance with how to troubleshoot this further would be very welcome!
The solution was to use the undocumented approach required due to changes a few months ago.
The contentful_rails gem requires contentful_model (and vice versa), and the only configuration documentation for contentful_model is in that project's README, describing the approach in my question. Configuration made in this way was then completely wiped when contentful_rails was initialized, which expected the configuration to be done in its own initializer.
So I have deleted config/initializers/contentful_model.rb and now my config/intializers/contents_rails.rb file looks like:
ContentfulRails.configure do |config|
config.authenticate_webhooks = true # false here would allow the webhooks to process without basic auth
config.webhooks_username = ENV['CONTENTFUL_WEBHOOK_USERNAME']
config.webhooks_password = ENV['CONTENTFUL_WEBHOOK_PASSWORD']
config.access_token = ENV['CONTENTFUL_ACCESS_TOKEN']
config.preview_access_token = ENV['CONTENTFUL_PREVIEW_ACCESS_TOKEN']
config.space = ENV['CONTENTFUL_SPACE']
config.contentful_options = {
#extra options to send to the Contentful::Client
}
end
Of note is that config.options is no longer a thing; it's config.contentful_options.

No terminal output with caypbara / rspec / selenium-webdriver

I have been writing rspec tests using capybara and selenium-webdriver. Almost without fail, whenever I run one of these tests, the console output is gone.
For instance:
~/code/code> bundle exec rspec spec/features/interactions_spec.rb
InteractionsSpec
~/code/code>
Thats just about all I ever see. The browser launches, performs the action's I've coded, but I don't see the usual output.
Sometimes I'll see this as output instead (yay!):
InteractionsSpec
login as admin works
login as user works
Finished in 16.84 seconds (files took 7.9 seconds to load)
2 examples, 0 failures
What could be causing the output from the tests to disappear? This makes it nearly impossible to write tests - as I have no idea what has run, what has passed, what has failed, or why they have failed.
I'm using these Gems, but performing a bundle update does not change the behavior.
capybara (2.4.4)
capybara-screenshot (1.0.3)
rspec (3.0.0)
rspec-activemodel-mocks (1.0.1)
rspec-collection_matchers (1.0.0)
rspec-core (3.0.3)
rspec-expectations (3.0.3)
rspec-mocks (3.0.3)
rspec-rails (3.0.2)
rspec-support (3.0.3)
selenium-webdriver (2.44.0)
shoulda-matchers (2.6.2)
Other involved software:
Firefox 33.1.1 (Chrome 39.0.2171.65 (64-bit) has the same behavior)
OSX 10.9.5
Rails 4.1.4
Update
This seemed to fix the problem for a while, even with a sleep of 1ms. However, that was only a temporary fix and this problem persists.
RSpec.configure do |config|
config.before(:each, :type => :feature) do
sleep(0.5)
end
end
Looks to me like the logger is getting silenced via a call to logger.silence() by some other thread.
When you put the sleep() in the code, then you yielded to some other thread that fixed the logging level - that's why it didn't matter how long the sleep was, it was the context switch that was fixing the issue.
When you upgraded the activesupport gem, this commit here https://github.com/rails/activerecord-session_store/commit/f92d1135fc620cb4d65239ef286b267945bbbbc6 fixes the issue (as you said). Reading that commit, notice a new threadsafe implementation of logger.silence. That's why it fixes your issue.
The old implementation of active_support/logger_silence.rb here isn't threadsafe:
require 'active_support/concern'
module LoggerSilence
extend ActiveSupport::Concern
included do
cattr_accessor :silencer
self.silencer = true
end
# Silences the logger for the duration of the block.
def silence(temporary_level = Logger::ERROR)
if silencer
begin
old_logger_level, self.level = level, temporary_level
yield self
ensure
self.level = old_logger_level
end
else
yield self
end
end
end
There seems to be a number of issues related to thread safety and race conditions in initializing the rails environment under capybara, which uses multiple threads. Rob's answer explains why it starts working (logging is fixed to be thread-save in activerecord-session_store).
It seems that these issues and commits are also related
https://github.com/rails/rails/issues/15089
https://github.com/jnicklas/capybara/issues/1419
https://github.com/jnicklas/capybara/commit/a4fc9d96f705e9ac4732c5fa05ab79d947743599

Resources