给出Rails的rspec视图规范,
RSpec.describe "plots/show", type: :view do
describe "displaying inoculation" do
context "inoculated" do
it "displays 'Yes'" do
assign(:plot, build(:plot))
render
expect(rendered).to have_content("Inoculated? Yes")
end
end
end
end
当我尝试将隐式主题样式与it {}
,
context "inoculated" do
before do
assign(:plot, build(:plot))
render
end
it { is_expected.to have_content("Inoculated? Yes") }
end
测试失败,期望找到文本"接种?是" in" inoculated" 。
我预计隐式主题将是视图,但主题是传递给context
的字符串。为什么不查看规范将视图作为规范的隐含主题?
答案 0 :(得分:2)
与视图规格无关。在确定默认主题时,它会尝试在嵌套描述链中查找类引用。如果找到,则采用最内层的类。如果没有找到,它会取最里面的值(大概是一个字符串)。
RSpec.describe User do
describe 'foo' do
describe 'bar' do
it { puts subject }
it { puts subject.class }
end
end
end
RSpec.describe 'User' do
describe 'foo' do
describe 'bar' do
it { puts subject }
it { puts subject.class }
end
end
end
RSpec.describe 'User' do
describe Project do
describe 'bar' do
it { puts subject }
it { puts subject.class }
end
end
end
#<User:0x007fe77593af80>
User
bar
String
#<Project:0x007fe7924f9170>
Project
一切都从这里开始,顺便说一下(如果你也倾向于做一些挖掘):
From: /Users/sergio/.gem/ruby/2.4.1/gems/rspec-core-3.6.0/lib/rspec/core/memoized_helpers.rb @ line 57:
Owner: RSpec::Core::MemoizedHelpers
Visibility: public
Number of lines: 6
def subject
__memoized.fetch_or_store(:subject) do
described = described_class || self.class.metadata.fetch(:description_args).first
Class === described ? described.new : described
end
end