情景
我已经提到了一个叫做的问题Taggable.它是一个允许任何模型支持标记的模块.我已经包含了这种担忧/模块到像模特User,Location,Places,Projects.
我想为这个模块编写测试,但不知道从哪里开始.
问题
1.我可以对此Taggable问题进行隔离测试吗?
在下面的示例中,测试失败,因为测试正在寻找a dummy_class table.我假设它正在这样做因为has_many代码,Taggable因此它期望'DummyClass'是一个ActiveRecord对象.
# /app/models/concerns/taggable.rb
module Taggable
extend ActiveSupport::Concern
included do
has_many :taggings, :as => :taggable, :dependent=> :destroy
has_many :tags, :through => :taggings
end
def tag(name)
name.strip!
tag = Tag.find_or_create_by_name(name)
self.taggings.find_or_create_by_tag_id(tag.id)
end
end
# /test/models/concerns/taggable_test.rb
require 'test_helpers'
class DummyClass
end
describe Taggable do
before do
@dummy = DummyClass.new
@dummy.extend(Taggable)
end
it …Run Code Online (Sandbox Code Playgroud) 我一直试图解决这个错误一段时间没有找到任何帮助,请帮助.
我正在尝试使用设计宝石在我的ROR应用上运行迁移.但我从ActiveSupport得到错误'持续时间无法强制转换为整数'.这发生了然后我尝试运行:
rake db:migrate
Run Code Online (Sandbox Code Playgroud)
我不确定它是否与迁移代码有关,或者它是否与新的设计版本有关.这是错误消息:
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/values/time_zone.rb:285: warning: circular argument reference - now
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/thread_safe-0.3.5/lib/thread_safe/cache.rb:155: warning: constant ::Fixnum is deprecated
rake aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'devise'.
Gem Load Error is: ActiveSupport::Duration can't be coerced into Integer
Backtrace for gem load error is:
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/core_ext/numeric/time.rb:50:in `*'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/core_ext/numeric/time.rb:50:in `days'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.1.1/lib/active_support/core_ext/numeric/time.rb:55:in `weeks'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/devise-4.2.0/lib/devise.rb:121:in `<module:Devise>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/devise-4.2.0/lib/devise.rb:9:in `<top (required)>'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `block (2 levels) in require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `each'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `block in require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `each'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `require'
/home/sam/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler.rb:106:in `require'
/home/sam/aplicaciones/pess/config/application.rb:7:in …Run Code Online (Sandbox Code Playgroud) 我不知道我是不是在做蠢事,所以请耐心等待。
tl; dr Rails ActiveSupport 时间和时区在 Alpine Linux 上似乎有错误。当它应该使用冬季时间时,它使用我的时区的 DST 变体(夏令时)。
重现步骤:
$ docker run -it --rm ruby:2.7.1-alpine sh
Run Code Online (Sandbox Code Playgroud)
以下所有步骤都发生在正在运行的 docker 容器内。
$ apk add --no-cache --update tzdata
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/1) Installing tzdata (2020c-r0)
Executing busybox-1.31.1-r9.trigger
OK: 23 MiB in 37 packages
Run Code Online (Sandbox Code Playgroud)
$ ruby -e 'puts Time.now.inspect'
2020-10-28 20:34:24.4817918 +0000
Run Code Online (Sandbox Code Playgroud)
看起来挺好的。时间以 UTC 打印。
Time类只能处理本地时间。这取决于本地系统时区,它可以使用操作系统配置机制进行配置,或者可以简单地通过TZenv var传递给 Ruby 。让我们尝试使用我的时区“欧洲/柏林”:$ TZ="Europe/Berlin" ruby -e …Run Code Online (Sandbox Code Playgroud) 我在我的代码中安装了'ActiveSupport'并且需要'active_support',但是当我尝试使用该Hash.from_xml()方法时,我得到了"No Method Error" .
我错过了什么?
$ gem list
Run Code Online (Sandbox Code Playgroud)
收益:
\*** LOCAL GEMS \***
activesupport (3.2.6)
bundler (1.1.4)
i18n (0.6.0)
json (1.7.3)
mime-types (1.19)
multi_json (1.3.6)
rake (0.9.2.2)
rest-client (1.6.7)
rubygems-bundler (1.0.3)
rvm (1.11.3.5)
Run Code Online (Sandbox Code Playgroud)
和:
$ ruby -v
Run Code Online (Sandbox Code Playgroud)
收益:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
Run Code Online (Sandbox Code Playgroud)
内容file.rb是:
require 'active_support'
require 'rest-client'
require 'json'
token = "xxx"
user = "xxx"
survey_id = "xxx"
responses_from_api = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php?Request=getLegacyResponseData&User=#{user}&Token=#{token}&Version=2.0&SurveyID=#{survey_id}&Format=XML"
responses = Hash.from_xml(responses_from_api).to_json
puts responses
Run Code Online (Sandbox Code Playgroud)
和:
$ ruby file.rb
Run Code Online (Sandbox Code Playgroud)
收益:
file.rb:8:in `<main>': …Run Code Online (Sandbox Code Playgroud) 我正在尝试记录我的REST API应用程序的请求.我正在使用rails通知,例如http://railscasts.com/episodes/249-notifications-in-rails-3
我无法理解如何解决rails通知的一个问题.
我的初始化代码
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
p name
p start
p finish
p id
p payload
end
Controller respond section
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
respond_to :json, :html
....
end
Run Code Online (Sandbox Code Playgroud)
控制器创建动作
def create
@post = Post.new(params[:post])
@post.save!
respond_with(@post, :location => nil)
end
Run Code Online (Sandbox Code Playgroud)
控制台输出
"process_action.action_controller"
2013-02-02 20:13:11 +0200
2013-02-02 20:13:11 +0200
"951b8999e9b71d4a8949"
{:controller=>"PostsController", :action=>"create", :params=>{"utf8"=>"?", "authenticity_token"=>"1WugY9gh6ZCRXjfBTuckye3c9XDvtCqMQ2JdBpCo88s=", "post"=>{"name"=>"post3", "title"=>"post3", "content"=>"post3"}, "commit"=>"Create Post", "action"=>"create", "controller"=>"posts"}, :format=>:html, :method=>"POST", :path=>"/posts", :status=>302, :view_runtime=>nil, :db_runtime=>0} …Run Code Online (Sandbox Code Playgroud) ruby-on-rails activesupport ruby-on-rails-3 ruby-on-rails-3.2
我正在使用Rails的ActiveJob,我的一个工作是将原始电子邮件作为输入.调试时,这可能会导致应用程序日志中出现大量噪音.我怎么能避免这种情况?
[ActiveJob] Enqueued EmailParserJob (Job ID: 9678f343-c876-4f9f-9cc7-db440634e178) to DelayedJob(default) with arguments: "NOISE"
Run Code Online (Sandbox Code Playgroud) 有人可以解释d1如何大于d2?他们是相同的日期(或至少他们看起来如何).
Loading development environment (Rails 3.0.8)
ruby-1.9.2-p180 :001 > d1 = Event.first.updated_at
=> Thu, 22 Sep 2011 02:24:28 PDT -07:00
ruby-1.9.2-p180 :002 > d2 = Time.zone.parse("2011-09-22T02:24:28-07:00")
=> Thu, 22 Sep 2011 02:24:28 PDT -07:00
ruby-1.9.2-p180 :003 > d1.class
=> ActiveSupport::TimeWithZone
ruby-1.9.2-p180 :004 > d2.class
=> ActiveSupport::TimeWithZone
ruby-1.9.2-p180 :005 > d1 > d2
=> true
ruby-1.9.2-p180 :006 >
Run Code Online (Sandbox Code Playgroud)
关于我的特定应用程序需求...我有一个iOS应用程序向我的Rails应用程序发出请求,传递一个JSON对象,其中包括格式为"2011-09-22T02:24:28-"的NSDates 07:00".我试图将该日期时间与ActiveSupport :: TimeWithZone类型的"updated_at"进行比较.
谢谢 - wg
我需要在我的Rails应用程序中运行以下代码:
ActiveSupport::TimeZone["Central Time (US & Canada)"].parse(game.date).utc.to_date.strftime("%_m/%d")[1..-1]
Run Code Online (Sandbox Code Playgroud)
游戏在哪里 @games.each do |game|
但这不起作用,我得到错误,TypeError: no implicit conversion of ActiveSupport::TimeWithZone into String.
但是,我可以运行:
ActiveSupport::TimeZone["Central Time (US & Canada)"].parse("2014-04-11 12am").utc.to_date.strftime("%_m/%d")[1..-1]
Run Code Online (Sandbox Code Playgroud)
返回"4/11"
如何使用上面的代码与`game.date'而不是硬编码的字符串?
编辑
游戏对象如下所示(来自db/seeds.rb):
Game.create(id: 9, date: "2014-04-11 12am", time: "705PM", opponent: "Jacksonville", away: false, event: "friday night fireworks")
Run Code Online (Sandbox Code Playgroud)
编辑2
在我做game.date的rails控制台中,它返回:
Fri, 11 Apr 2014 00:00:00 UTC +00:00
Run Code Online (Sandbox Code Playgroud)
所以它似乎不是一个字符串.
我有这个代码application controller:
# Method to capture and handle all exceptions
rescue_from Exception do |ex|
Rails.logger.debug ex
do_stuff(ex)
end
Run Code Online (Sandbox Code Playgroud)
我想把它移到一个模块然后:
class ApplicationController < ActionController::Base
include 'module'
...
Run Code Online (Sandbox Code Playgroud)
现在我的模块看起来像:
# lib/exception_mailer.rb
require 'action_mailer'
require 'active_support'
module ExceptionMailer
# Method to capture and handle all exceptions
rescue_from Exception do |ex|
...
Run Code Online (Sandbox Code Playgroud)
我得到了: undefined method 'rescue_from' for ExceptionMailer:Module
我用Google搜索了"我如何在模块中包含rescue_from?" - 我还有点失落.
activesupport ×10
ruby ×7
xml ×2
alpine-linux ×1
compare ×1
datetime ×1
devise ×1
hash ×1
json ×1
minitest ×1
module ×1
time ×1
unit-testing ×1