0

我正在使用奇妙的wisper gem对模型的变化做出反应(Rails 4.1)。

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

end

broadcastWisper::Publisher模块提供的一个方法,如您在上面看到的,它的包含被注释掉了。

由于这一切,我不明白如何通过这样的测试而不抱怨“broadcast缺少方法”:

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

正如预期的那样,development/production环境确实抱怨缺少方法

- 更新 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"
4

1 回答 1

0

你当然可以这样做:

module MyWisper
  def broadcast
    puts 'hello'
  end
end

class SomeClass
  include MyWisper
end

SomeClass.new.broadcast

--output:--
hello

但你也可以这样做:

module MyWisper
  def broadcast
    puts 'hello'
  end
end

def broadcast
  puts 'hi'
end

class SomeClass
  #include MyWisper
  broadcast
end


--output:--
hi

因此,在所有要求的某个地方,您需要一个全局广播()方法进入您的测试环境。

于 2016-07-13T20:54:55.657 回答