将常量或带引号的字符串传递给类名的rspec是否有区别?
常数:
require 'spec_helper'
describe Match do
end
引用:
require 'spec_helper'
describe "Match" do
end
答案 0 :(得分:3)
describe
的第一个参数是该示例组的描述。但是,按https://www.relishapp.com/rspec/rspec-core/v/3-0/docs/subject/implicitly-defined-subject:
如果最外面的示例组的第一个参数是一个类,则为 该类的实例通过主题暴露给每个示例 方法
它还补充说:
虽然以下示例演示了如何将主题用作a 面向用户的概念,我们建议您保留它以支持 隐藏其使用的自定义匹配器和/或扩展库 实例
行为规则比上面暗示的要复杂一些,但至少从RSpec 2.99开始。例如,仍然通过subject方法公开非类参数,但最里面的示例组优先。在adition中,即使它不是最外面的示例组,也会应用关于实例化类的规则。
以下通过的例子说明了更多的案例:
# 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