小编bre*_*ter的帖子

将YAML与变量一起使用

YAML文件中的变量是否可能?例如:

theme:
  name: default
  css_path: compiled/themes/$theme.name
  layout_path: themes/$theme.name
Run Code Online (Sandbox Code Playgroud)

在这个例子中,如何theme: name: default在其他设置中使用?语法是什么?

ruby variables yaml ruby-on-rails string-interpolation

75
推荐指数
3
解决办法
10万
查看次数

activerecord迁移中序列化列的默认值

所以我有一个序列化的列:维度,在我的迁移中,我想将该字段设置为默认哈希.

我试过了...

create_table :shipping_profiles do |t|
      t.string      :dimensions_in, :default => {:width => 0, :height => 0, :depth => 0}
Run Code Online (Sandbox Code Playgroud)

只是

t.string :dimensions_in, :default => Hash.new()
Run Code Online (Sandbox Code Playgroud)

但是这些字段最终为空.如何在创建时为此字段设置默认的序列化对象,或者至少确保我的serialize属性始终是哈希?

migration activerecord ruby-on-rails

18
推荐指数
3
解决办法
1万
查看次数

如何用玩笑取消模拟单个实例方法

来自 rspec,我无法理解开玩笑的嘲弄。我正在尝试的方法是自动模拟一个类的构造函数及其所有函数,然后将它们一个一个地取消模拟以仅测试该函数。我能找到的唯一文档是使用 2 个类,模拟 1 个类,然后测试这些函数是否是从另一个未模拟的类调用的。

下面是我正在尝试做的一个基本的、人为的想法。有人可以指导我以开玩笑的方式这样做吗?

foo.js

class Foo
  constructor: ->
    this.bar()
    this.baz()
  bar: ->
    return 'bar'
  baz: ->
    return 'baz'
Run Code Online (Sandbox Code Playgroud)

foo_test.js

// require the class
Foo = require('foo')

// mock entire Foo class methods
jest.mock('foo')

// unmock just the bar method
jest.unmock(Foo::bar)

// or by
Foo::bar.mockRestore()

// and should now be able to call
foo = new Foo
foo.bar() // 'bar'
foo.baz() // undefined (still mocked)

// i even tried unmocking the instance
foo = new Foo
jest.unmock(foo.bar) …
Run Code Online (Sandbox Code Playgroud)

mocking ecmascript-6 jestjs es6-class

16
推荐指数
3
解决办法
1万
查看次数

capimbara/selenium与rspec之前:所有钩子

为了减少使用selenium的页面访问次数,我想从一个before :all钩子调用visit方法,并在单个页面加载时运行我的所有示例.但是,当我指定before :allvs时before :each,浏览器会打开,但永远不会访问该URL.以下是一个简化的人为例子......

describe 'foobar', :js => true do
  before :all do
    Capybara.default_wait_time = 10
    obj = Factory(:obj)
    visit obj_path(obj)
  end

  it 'should have foo' do
    page.should have_content('foo')
  end

  it 'should have bar' do
    page.should have_content('bar')
  end
end
Run Code Online (Sandbox Code Playgroud)

当我设置它时before :each,它可以工作,但页面加载两次.这是Capybara的限制吗?

selenium capybara rspec2

13
推荐指数
1
解决办法
2459
查看次数

如何用rspec测试ruby继承

我试图测试在类继承期间运行的逻辑,但在运行多个断言时遇到了问题.

我第一次尝试......

describe 'self.inherited' do
  before do
    class Foo
      def self.inherited klass; end
    end

    Foo.stub(:inherited)

    class Bar < Foo; end
  end

  it 'should call self.inherited' do
    # this fails if it doesn't run first
    expect(Foo).to have_received(:inherited).with Bar
  end

  it 'should do something else' do
    expect(true).to eq true
  end
end
Run Code Online (Sandbox Code Playgroud)

但是这会失败,因为Bar类已经加载,因此不会inherited第二次调用.如果断言没有先运行......它就失败了.

那么我尝试了类似......

describe 'self.inherited once' do
  before do
    class Foo
      def self.inherited klass; end
    end

    Foo.stub(:inherited)

    class Bar < Foo; end
  end

  it 'should call self.inherited' do
    @tested ||= …
Run Code Online (Sandbox Code Playgroud)

ruby rspec

12
推荐指数
2
解决办法
7909
查看次数

mongoid找到VS在哪里

当我通过ID搜索时,我似乎只对1个特定模型有这个问题

>> Cart.where(:_id => '4dae5902e1607c232c000009').first
=> #<Cart _id: 4dae5902e1607c232c000009, _id: BSON::ObjectId('4dae5902e1607c232c000009'), _type: nil>
>> Cart.find('4dae5902e1607c232c000009')
Mongoid::Errors::DocumentNotFound: Document not found for class Cart with id(s) 4dae5902e1607c232c000009.
Run Code Online (Sandbox Code Playgroud)

奇怪的是,与其他型号,我可以使用发现就好了.有任何想法吗?

堆栈的其余部分是......

from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:192:in `execute_or_raise'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:190:in `tap'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:190:in `execute_or_raise'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:106:in `find'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/finders.rb:67:in `find'
from (irb):37
Run Code Online (Sandbox Code Playgroud)

criteria find where mongoid

11
推荐指数
1
解决办法
3857
查看次数

与coffeescript和Rails资产管道的JSON错误

我正在使用Rails 3.2.14而没有问题......

我最近重命名application.jsapplication.js.coffee,现在收到JSON错误.

JSON::GeneratorError

only generation of JSON objects or arrays allowed
  (in /.../app/assets/javascripts/application.js.coffee)
Run Code Online (Sandbox Code Playgroud)

即使我删除了所有内容,application.js.coffee我仍然会收到错误.

当我尝试直接查看它(http://localhost:3000/assets/application.js),它同样的问题:

throw Error("JSON::GeneratorError: only generation of JSON objects or arrays allowed\n  (in /.../app/assets/javascripts/application.js.coffee)")
Run Code Online (Sandbox Code Playgroud)

我已针对任何潜在问题梳理了我的应用程序,但一切看起来非常标准.

ruby-on-rails coffeescript asset-pipeline

10
推荐指数
2
解决办法
1920
查看次数

rails 3验证器&i18n

所以我刚开始在rails 3中使用自定义验证器,但是我不确定我是否可以使用我现有的activerecord i18n语言环境文件.似乎我必须这样做

record.errors[attribute] << I18n.t('activerecord.errors.models.{model}.attributes.{attribute}.invalid_whatever') if ...
Run Code Online (Sandbox Code Playgroud)

而不是之前,我可以完成

:message => :invalid_whatever
Run Code Online (Sandbox Code Playgroud)

是否有我可以在我的ActiveModel:Validator/EachValidator类中使用的简写来完成同样的事情?

validation internationalization ruby-on-rails-3

8
推荐指数
1
解决办法
2634
查看次数

运行 yarn install 时,一个模块不提供另一个模块是什么意思?

当我yarn在使用 firebase 的 React 应用程序上运行时,我会收到几个警告,例如...

@firebase/auth@npm:0.14.5 [c52f6] doesn't provide @firebase/app-types@0.x requested by @firebase/auth-types@npm:0.10.0

myapp@workspace:. doesn't provide @testing-library/dom@>=5 requested by @testing-library/user-event@npm:10.2.0
Run Code Online (Sandbox Code Playgroud)

(仅供参考...我正在使用纱线 v2)

这是否意味着我需要将它们显式添加到我的 package.json 中?

node.js npm yarnpkg yarnpkg-v2

8
推荐指数
1
解决办法
585
查看次数

rails 3.1一些资产未在生产时进行预编译

我遇到了Heroku的问题,显示我的'places.js'没有预编译,即使我在雪松堆栈上运行,并且在slug编译期间它正在运行rake预编译任务.所以我尝试在本地运行它rake assets:precompile RAILS_ENV=production,事实上rails并没有预编译我的/app/assets/javascripts/places.js.coffee.erb资产.

production.rb正在使用默认的rails 3.1配置,我甚至尝试从资产中删除.erb,但无济于事.

我还想过,因为我的places.js.coffee.erb资产不包含在链轮清单中(我手动将其包含在我的应用程序中),也许它只预编译清单中的资产.在清单中要求它也不起作用.

只有我application.js.coffee和`application.css正在预编译(有和没有摘要).

我发现的唯一问题可能是用于匹配资产的差的正则表达式,但默认值(?:\/|\\|\A)application\.(css|js)$与我的资产不匹配,因此应该包含它.

我不知道如何从这里排除故障.一切都是默认的.关于这里可能发生什么的任何想法?

ruby-on-rails-3.1 asset-pipeline

6
推荐指数
1
解决办法
5931
查看次数