我在代码中重写了这个问题:
many = 1000
# An expensive method.
#
# It returns some data or nil if no result is available.
expensive_method = lambda do
rand(5) == 0 ? nil : "foo"
end
# Now, let's collect some data and stop collecting when no more data is
# available.
# This is concise but doesn't work.
collection = many.times.map do
expensive_method.call || break
end
puts collection.is_a? Array # false
# This is less concise but works.
collection = []
many.times …
Run Code Online (Sandbox Code Playgroud) 在没有剔除前者的情况下,在RSpec中编写与Resque相关的规范的最佳方法是什么?
我们目前使用以下帮助器:
@dir = File.dirname(File.expand_path(__FILE__))
def start_redis
`redis-server #{@dir}/redis-test.conf`
Resque.redis = "localhost:9736"
end
def stop_redis
`rm -f #{@dir}/dump.rdb`
pid = `ps -A -o pid,command | grep [r]edis-test`.split(" ")[0]
Process.kill("KILL", pid.to_i)
end
Rspec.configure do |config|
config.before(:suite) do
start_redis
end
config.after(:suite) do
stop_redis
end
config.before(:each) do
Resque.redis.flushall
end
end
Run Code Online (Sandbox Code Playgroud)
从Resque自己的测试帮助中大量借用,这种方法很好但是zsh: killed rake
当整个规范套件通过rake运行时会发出烦人的声音.
我在命名空间类中包含模块时遇到问题.以下示例将引发错误uninitialized constant Bar::Foo::Baz (NameError)
.我在这里缺少什么基本的Ruby知识?
module Foo
module Baz
def hello
puts 'hello'
end
end
end
module Bar
class Foo
include Foo::Baz
end
end
foo = Bar::Foo.new
Run Code Online (Sandbox Code Playgroud) 这是一个简短的问题:我正在寻找一种在调试模式下使用-u
交换机运行规范的方法,这样RSpec会在失败时掉到控制台,而不必debugger
在代码中添加一行.有什么指针吗?
我试图用rspec2模拟一个视图助手.执行此操作的旧方法会引发错误,抱怨模板对象未定义:
template.should_receive(:current_user).and_return(mock("user"))
Run Code Online (Sandbox Code Playgroud)
我在这里遗漏了什么,或者这还没有在rspec2中实现(还)?
假设我有一个带有视图助手方法的东西资源,例如:
module ThingsHelper
def foo
ret = ""
3.times { ret += content_tag(:li, "foo") }
content_tag(:ul, ret)
end
end
Run Code Online (Sandbox Code Playgroud)
然后,在模板中使用它:
%p
= foo
Run Code Online (Sandbox Code Playgroud)
生成的HTML源代码如下所示:
<!DOCTYPE html>
<html>
<head>
<title>Foo</title>
</head>
</html>
<body>
<p>
<ul><li>foo</li><li>foo</li><li>foo</li></ul>
</p>
</body>
Run Code Online (Sandbox Code Playgroud)
如您所见,辅助输出不会缩进为其余代码.有什么方法可以解决这个问题?
我通常使用此步骤使用factory_girl设置记录:
Given /^the following (.+) records?:$/ do |factory, table|
table.hashes.each do |hash|
Factory(factory, hash)
end
end
Run Code Online (Sandbox Code Playgroud)
这是我在设置关联时的解决方法:
Given the following group record:
| id | name |
| 1 | foo |
And the following item records:
| name | group_id |
| bar | 1 |
| baz | 1 |
# ...
Run Code Online (Sandbox Code Playgroud)
我知道这很糟糕.使用id会使域名人员的优势变得脆弱和神秘.
所以,我的问题是 - 与factory_girl建立关联以及如上所述的表参数的最佳做法是什么?
在下面的测试中,Bar和Baz块包含相同的规格.
暂且不说为什么首先需要这样的重复,我想知道如何干这个.
我尝试将块转换为对象并在Bar和Baz下调用它们,但可能是因为我没有使示波器正确,我无法使其工作.
describe Foo do
describe Bar do
before(:each) do
prepare
end
it "should do something" do
true
end
it "should do something else" do
true
end
end
describe Baz do
before(:each) do
prepare_something_else
end
it "should do something" do
true
end
it "should do something else" do
true
end
end
end
Run Code Online (Sandbox Code Playgroud) 听起来很基本,我不能将date_helper默认为日期,如:
- semantic_form_for resource do |f|
- f.inputs do
= f.input :issued_on, :default => Date.today
= f.buttons
Run Code Online (Sandbox Code Playgroud)
如果资源没有日期,上面只会呈现空白列.
我会理解任何我可能做错的指针.
foo, bar = 1, 0/0 rescue 0, 0 # this won't work
foo.should eql 0
bar.should eql 0
Run Code Online (Sandbox Code Playgroud)
我怎么去做这个通行证?
我设法让Cucumber,RSpec,Guard和Spork在Rails 3应用程序中相互配合,但现在我遇到了上述问题.
目前我require 'spork/ext/ruby-debug'
在prefork区块,spec_helper.rb
但这只有在我手动运行spork,没有后卫时才有效.
我一直试图干掉以下与字符串中的hashtags匹配但没有成功的正则表达式:
/^#(\w+)|\s#(\w+)/i
Run Code Online (Sandbox Code Playgroud)
这不起作用:
/^|\s#(\w+)/i
Run Code Online (Sandbox Code Playgroud)
不,我不想在开头用逗号进行交替:
/(^|\s)#(\w+)/i
Run Code Online (Sandbox Code Playgroud)
我在Ruby中这样做 - 尽管我认为这不应该是相关的.
举一些匹配和不匹配字符串的例子:
'#hashtag it is' # should match => [["hashtag"]]
'this is a #hashtag' # should match => [["hashtag"]]
'this is not a#hashtag' # should not match => []
Run Code Online (Sandbox Code Playgroud)
有什么建议?我在挑剔吗?