在我的 Rails 应用程序中,我大量使用模型目录的子目录,因此使用命名空间模型 - 也就是说,我有以下目录层次结构:
models
|
+- base_game
| |
| +- foo.rb (defines class BaseGame::Foo)
|
+- expansion
|
+- bar.rb (defines class Expansion::Bar)
Run Code Online (Sandbox Code Playgroud)
这是由 Rails 强制执行的 - 也就是说,将类称为“只是”Foo和Bar.
我想对这些类进行单元测试(在 ActiveSupport::TestCase 之上使用 Shoulda)。我知道被测类的名称将派生自测试用例类的名称。
我如何编写单元测试来测试这些类?
我一直在尝试建立一个系统,我可以生成一系列类似的Ruby类,用整数参数区分,然后将其保存到相关类的类变量中 - 类似于C++模板.
但是,引用(因此,创建)模板化类的新版本会覆盖先前版本中保存的参数,而我无法解决原因.
这是一个最小的例子
class Object
def self.const_missing(name)
if name =~ /^Templ(\d+)$/
return make_templ $1.to_i
else
raise NameError.new("uninitialised constant #{name}")
end
end
private
def make_templ(base)
# Make sure we don't define twice
if Object.const_defined? "Templ#{base}"
return Object.const_get "Templ#{base}"
else
# Define a stub class
Object.class_eval "class Templ#{base}; end"
# Open the class and define the actual things we need.
Object.const_get("Templ#{base}").class_exec(base) do |in_base|
@@base = in_base
def initialize
puts "Inited with base == #{@@base}"
end
end
Object.const_get("Templ#{base}")
end
end
end …Run Code Online (Sandbox Code Playgroud) 如何检查浏览器是否支持元素的HTML5 form属性input?
在这个问题之后,我尝试了以下方法:
var supportForm = function()
{
var input = document.createElement("input");
if ("form" in input)
{
input.setAttribute("form", "12345");
if (input.form == "12345")
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
......但这给FireFox带来了假阴性(至少14).更换input.form用input.getAttribute("form")给出了IE 9的假阳性.
我有一个黄瓜测试套件为我的Rails应用程序,包括大约500个场景,它们之间约5000步.
我已经设置了我的github存储库以使用Travis-CI,使用以下内容.travis.yml.
language: ruby
rvm:
- "1.9.2"
script:
- RAILS_ENV=test bundle exec rake db:migrate --trace
- bundle exec cucumber -f progress -r features features/cards/base_game
- bundle exec cucumber -f progress -r features features/cards/basic_cards
- bundle exec cucumber -f progress -r features features/cards/intrigue
- bundle exec cucumber -f progress -r features features/cards/seaside
- bundle exec cucumber -f progress -r features features/cards/prosperity
- bundle exec cucumber -f progress -r features features/cards/interactions
before_script:
- cp config/database.travis.yml config/database.yml
- psql …Run Code Online (Sandbox Code Playgroud) 我想向您展示我的用例,然后讨论可能的解决方案:
问题A:我有2个食谱,"a"和"b".."a"在我的文件系统上安装一些程序(比如"/usr/local/bin/stuff.sh"和食谱"b"需要运行这和输出做了些什么.
所以食谱"a"看起来像:
execute "echo 'echo stuff' > /usr/local/bin/stuff.sh"
Run Code Online (Sandbox Code Playgroud)
(该脚本只是回显(st)"stuff"到stdout)
和食谱"b"看起来像:
include_recipe "a"
var=`/usr/local/bin/stuff.sh`
Run Code Online (Sandbox Code Playgroud)
(注意反引号,var应该包含stuff)
现在我需要用它做一些事情,例如用这个用户名创建一个用户.所以在脚本"b"我添加
user "#{node[:var]}"
Run Code Online (Sandbox Code Playgroud)
碰巧,这不起作用..显然厨师运行所有不是资源的东西,然后运行资源,所以一旦我运行脚本厨师抱怨它无法编译,因为它首先尝试运行"var = ..."在配方行"b"并失败,因为配方a的"执行..."尚未运行,因此"stuff.sh"脚本尚不存在.毋庸置疑,这非常令人讨厌,因为它打破了"厨师从上到下按顺序运行",这是我在开始使用它时所承诺的.但是,我不是很挑剔,所以我开始寻找这个问题的替代解决方案,所以:
问题B:我碰到了"ruby_block"的想法.显然,这是一种资源,因此它将与其他资源一起进行评估.我说好了,然后我想创建脚本,在"ruby_block"中获取输出,然后将其传递给"user".所以食谱"b"现在看起来像:
include_recipe "a"
ruby_block "a_block" do
block do
node.default[:var] = `/usr/local/bin/stuff.sh`
end
end
user "#{node[:var]}"
Run Code Online (Sandbox Code Playgroud)
但是,事实证明变量(var)没有从"ruby_block"传递给"user"而且它仍然是空的.无论我试图用它做什么杂耍,我都失败了(或者我只是没找到正确的杂耍方法)
给大厨/红宝石大师们:我如何解决问题A?我如何解决问题B?
考虑以下Ruby代码:
[1,3].any? &:even? || true
# => false
[1,3].any? &nil || :even?
# => false
[1,3].any? &nil || :odd?
# => true
Run Code Online (Sandbox Code Playgroud)
所以看起来布尔 - 或者||具有比to-proc一元更高的优先级&.我没想到这一点.这是对的,它是否记录在任何地方?
我正在尝试创建一个双色背景的HTML表格单元格; 所以我在背景上有正常的文字,左边是黄色,右边是绿色.
我到目前为止最接近的如下.背景是正确的一半,但内容文本位于其下方.
<html>
<head>
<style type='text/css'>
td.green
{
background-color: green;
padding: 0px;
margin: 0px;
height:100%;
text-align:center
}
div.yellow
{
position:relative;
width: 50%;
height: 100%;
background-color:yellow
}
</style>
</head>
<body style="width: 100%">
<table style="width: 25%">
<tr style="padding: 0px; margin: 0px">
<td class="green">
<div class="yellow"></div>
<div class="content">Hello</div>
</td>
</tr>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
好吧,我很困惑.我试图在Rails 3.1下使用带有Test :: Unit的shoulda,之前已成功使用Rails 2.3.11.
我的Gemfile中有以下内容:
group :test do
gem 'shoulda'
end
Run Code Online (Sandbox Code Playgroud)
(我跑了bundle install- bundle show shoulda表演c:/Ruby192/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3)
我有以下test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'shoulda'
class ActiveSupport::TestCase
end
Run Code Online (Sandbox Code Playgroud)
以及user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
should validate_presence_of :email
should validate_format_of(:email).with("user+throwaway@subdom.example.com").with_message(/valid email address/)
should validate_presence_of(:encrypted_password)
should validate_confirmation_of :password
end
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时ruby -Itest test\unit\user_test.rb,我收到以下错误:
test/unit/user_test.rb:4:in `<class:UserTest>': undefined method `validate_presence_of' for UserTest:Class (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
我没有正确设置什么?
我有类似于以下的代码:
void generateInt(int *result)
{
/* chosen by fair dice roll*/
*result = 1;
}
typedef enum colour
{
RED,
GREEN,
BLUE
} COLOUR;
int main(int, char**)
{
COLOUR col, col2;
int i;
generateInt(&i);
col = (COLOUR)i;
generateInt((int*)(&col2));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在第二次调用 之后generateInt,是否保证col和col2都等于GREEN?我知道第一个版本设置col是合法的,但我不知道它是否定义了如果将指向枚举的指针转换为指向整数的指针,然后通过指针进行分配会发生什么。
我发现Microsoft Visual C++编译器和gcc-4.8.1(由ideone.com提供)之间存在差异.考虑以下SSCCE:
struct S
{
int x;
};
class A
{
public:
int x;
A(const S& s) : x(s.x) {}
};
class B
{
int x, y;
public:
template <typename T> explicit B(const T& t) : x(t.x), y(t.y) {}
B(const A& a) : x(a.x), y(0) {}
};
int main() {
S s = {1};
B b1 = s; // Compiles OK on MSVC++;
// Fails on gcc - conversion from ‘S’ to non-scalar type ‘B’ requested
B b2(s); // …Run Code Online (Sandbox Code Playgroud) ruby ×3
shoulda ×2
c ×1
c++ ×1
casting ×1
chef-infra ×1
chef-solo ×1
constructor ×1
css ×1
cucumber ×1
enums ×1
gcc ×1
html ×1
html-table ×1
html5 ×1
javascript ×1
namespaces ×1
travis-ci ×1
unit-testing ×1
variables ×1
visual-c++ ×1