0

I'm using the fantastic wisper gem to react to changes in my models(Rails 4.1).

Class Transaction < ActiveRecord::Base
  #include Wisper::Publisher
  after_save { broadcast(:transaction_saved)}

end

Here broadcast is a method provided by the Wisper::Publisher module, the inclusion of which is commented out as you can see above.

Due to all this, I'm not understanding how a test like this one can pass without complaining about "broadcast method missing":

require 'test_helper'
class TransactionTest < ActiveSupport::TestCase
  include GeneralHelper # this doesnt include any other module
  def setup
    @tr = Transaction.create(...)
  end
  def test_transaction_method
    @tr.update_attributes(amount:100)
  end
end

As expected development/production environments do complain about method missing

-- Update test/test_helper.rb

ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "mocha/mini_test"
require 'minitest/reporters'
Minitest::Reporters.use!(
  Minitest::Reporters::ProgressReporter.new,
  ENV,
  Minitest.backtrace_filter)

# Capybara and poltergeist integration
require "capybara/rails"
require "capybara/poltergeist"
Capybara.javascript_driver = :poltergeist


class ActiveSupport::TestCase
  ActiveRecord::Migration.check_pending!
  include FactoryGirl::Syntax::Methods

  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  fixtures :all

  # Add more helper methods to be used by all tests here...
  def assert_valid(model, msg = nil)
    msg = message(msg) { "Expected #{model} to be valid, but got errors: #{errors}." }
    valid = model.valid?
    errors = model.errors.full_messages.join(', ')
    assert valid, msg
  end
end

Gemfile

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.13'

# Use pg as the database for Active Record
gem 'pg'

# Use SCSS for stylesheets
gem 'sass-rails'#, '~> 4.0.2'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-turbolinks'

#gem 'bootstrap-sass'
gem 'font-awesome-rails'
gem 'simple-line-icons-rails'
gem 'active_link_to'

gem 'country_select', github: 'stefanpenner/country_select'
gem 'chronic'

gem 'will_paginate'#, '3.0.4'
gem 'will_paginate-bootstrap'


#Editable
#gem 'bootstrap-editable-rails'
#Encryption
gem 'aes'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

gem 'redis'
gem 'firebase'
# Use unicorn as the app server
gem 'puma'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

gem 'recipient_interceptor'

group :test, :development do
  #gem 'rspec-rails'
  #gem 'rspec-its'
  gem 'pry'
  gem 'pry-nav'
  gem 'pry-rails'
end

gem 'nokogiri'
gem 'faker'
gem 'haml'
gem 'slim'
gem 'rack-mini-profiler'
gem 'newrelic_rpm'
#gem 'appsignal'

group :test do
  gem 'capybara'
  #gem 'capybara-webkit'
  gem 'factory_girl_rails'
  #gem "connection_pool"
  #gem "launchy"
  gem "minitest"
  gem "minitest-reporters"
  gem "mocha"
  gem "poltergeist"
  #gem "shoulda"
end

gem 'axlsx'
gem 'prawn'
gem 'active_model_serializers'

gem 'card-rails'

gem 'rails_12factor', group: :production
#gem 'twitter-typeahead-rails', :git => "git://github.com/yourabi/twitter-typeahead-rails.git"
gem 'rack-cors', :require => 'rack/cors'

gem 'sidekiq'
gem 'rest-client'
gem 'nest'
gem 'redic'
gem 'fog'
gem 'aws-sdk'
gem 'twilio-ruby'
gem 'react-rails', '~> 1.0'
gem 'wisper', '2.0.0.rc1'
gem 'ruby-prof'
gem 'le'
gem 'mechanize'
gem 'mandrill-api'
gem 'business_time'
gem 'paleta'
gem "aftership", "~> 4.3.1"
lllllll
  • 4,715
  • 6
  • 29
  • 42
  • Post your Gemfile. Post test_helper.rb – 7stud Jul 13 '16 at 20:22
  • @7stud you're right, done! – lllllll Jul 13 '16 at 20:37
  • It's helpful to future readers to post an answer, rather than leaving the question unresolved – max pleaner Jul 13 '16 at 20:50
  • I'd put `raise` in the `after_save` to make sure it is actually being called in the test. – Kris Jul 14 '16 at 14:48
  • 1
    You can also check if `Transaction` is including the Wisper module with `Transaction.ancestors.include?(Wisper::Publisher)`. – Kris Jul 14 '16 at 14:54
  • t@Kris It is being called for sure, since I set some debugger breakpoint there. `Transaction.ancestors` does indeed contain `Wisper::Publisher`! I wonder whether *all* helpers in the `app/helpers` folders are added to the test-suite? That would explain it since some helper modules include `Wisper::Publisher`. Thanks! – lllllll Jul 14 '16 at 17:09
  • As far as I am aware helpers wouldn't normally be included in a model, not automatically anyway. – Kris Jul 15 '16 at 15:03
  • did you solve this? – Kris Sep 22 '17 at 09:38
  • Hi @Kris , unfortunately I can't remember right now the ending to this inquiry. I take note to re-visit this asap and get back to you. – lllllll Sep 22 '17 at 10:00

1 Answers1

0

You can certainly do this:

module MyWisper
  def broadcast
    puts 'hello'
  end
end

class SomeClass
  include MyWisper
end

SomeClass.new.broadcast

--output:--
hello

But you can also do this:

module MyWisper
  def broadcast
    puts 'hello'
  end
end

def broadcast
  puts 'hi'
end

class SomeClass
  #include MyWisper
  broadcast
end


--output:--
hi

So somewhere in all the requires, you are requiring a global broadcast() method into your test environment.

7stud
  • 46,922
  • 14
  • 101
  • 127
  • However the `broadcast` method is doing exactly what Wisper's broadcast is doing. Besides the method comes with a parameter eg `transaction_saved`, which is also submitted(I tested debugging inside the observer method that listens to `transaction_saved` broadcasts). So `Wisper::Publisher` must be included somewhere that I'm missing, only for `test` env. – lllllll Jul 13 '16 at 20:58
  • I wouldn't define methods outside of a class/module, they will populate everything. – Kris Sep 22 '17 at 09:37