ruby-on-rails - 在 RSpec 中引用类名

标签 ruby-on-rails ruby testing rspec

将常量或带引号的字符串传递给类名的 rspec 有什么区别吗?

常数:

require 'spec_helper'

describe Match do
end

引用:

require 'spec_helper'

describe "Match" do
end

最佳答案

describe 的第一个参数是该示例组的描述。但是,根据 https://www.relishapp.com/rspec/rspec-core/v/3-0/docs/subject/implicitly-defined-subject :

If the first argument to the outermost example group is a class, an instance of that class is exposed to each example via the subject method.

它还添加了:

While the examples below demonstrate how subject can be used as a user-facing concept, we recommend that you reserve it for support of custom matchers and/or extension libraries that hide its use from examples.

行为规则比上面暗示的要复杂一点,但是,至少从 RSpec 2.99 开始是这样。例如,非类参数仍然通过 subject 方法公开,但最里面的示例组优先。此外,关于实例化类的规则将适用,即使它不是最外层的示例组。

以下传递示例说明了更多情况:

# simple case for class
describe Object do
  specify {subject.should be_an_instance_of(Object)}
end

# simple case for non-class
describe "String" do
  specify {subject.should be_eql("String")}
end

# nested non-class arguments; inner takes precedence
describe "Outer String" do
  describe "String" do
    specify {subject.should be_eql("String")}
  end
end

# nested class arguments; outer takes precedence
describe Object do
  describe Module do
    specify {subject.should be_an_instance_of(Object)}
  end
end

# class inside non-class; class takes precedence
describe "String" do
  describe Object do
    specify {subject.should be_an_instance_of(Object)}
  end
end

# class inside explicit subject; explicit subject takes precedence
describe "Outer string" do
  subject {"String"}
  describe Object do
    specify {subject.should be_eql("String")}
  end
end

# class outside explicit subject; explicit subject takes precedence
describe Object do
  subject {"String"}
  describe Module do
    specify {subject.should be_eql("String")}
  end
end

关于ruby-on-rails - 在 RSpec 中引用类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20939175/

相关文章:

Ruby:自定义 gem 需要 'require' 用于其中的模块

ruby - failedPrecondition : Must be a G Suite domain user. 使用服务帐户管理 ruby​​ 中的联系人

ruby-on-rails - 测试我的 Ruby on Rails 应用程序

ruby-on-rails - Ruby:如何将对象更改为可读的内容

ruby-on-rails - 使用 active_model_serializers 序列化权限(例如 CanCan)

ruby-on-rails - 如何将 2 个 Rails 模型混合到一个查找中?

PHP 5 : return by ref method produces unexpected results

visual-studio-2010 - VS 2010 : Pass results of a TestMethod to another Testmethod

ruby-on-rails - 在 Ubuntu 中运行 rails s 命令时出错

ruby-on-rails - 旧 Rails 应用程序出现奇怪的 Rails 错误 "permission denied: bin/rails"