可以在rspec中测试复数函数吗?
let(:schedule) { FactoryGirl.create(:schedule) }
Failure/Error: it { should have_selector('h1', text: pluralize(Schedule.count.to_s, "schedule")) }
NoMethodError:
undefined method `pluralize' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xb607068c>回答:(正如Eric C所指出的那样)
describe SchedulesHelper do
describe "pluralize schedule" do
if(Schedule.count > 0)
it { pluralize(1, "schedule").should == "1 schedule" }
else
it { pluralize(0, "schedule").should == "0 schedules" }
end
end
end发布于 2013-01-15 00:56:42
你问题的答案是..。说大也大吧。Rails提供了使用复数和使用rspec- rails的功能,假设您正在使用rspec-rails,使您能够在需要时调用rails方法。如果这是一个视图测试,就像它看起来那样,您可以键入如下内容:
it { should have_selector('h1', text: view.pluralize(Schedule.count.to_s, "schedule")) }这里的关键是您在之前添加了视图.。
我想强调的是,在测试时,在看似视图测试的情况下测试辅助方法并不是一个好主意。如果你真的想测试复数本身,最好在helper spec中测试它。做一些类似的事情:
it { pluralize(1, "schedule").should == "1 schedule" }
it { pluralize(0, "schedule").should == "0 schedules" }通过这种方式,您可以确定结果,并在其他测试中进行假设,然后测试适当的结果。这实际上将不可避免地使测试变得更好,因为如果像make这样的辅助程序发生更改,那么您将有两个测试来警告此更改。然后,您可以相应地进行调整。这只是个想法。
发布于 2013-09-19 22:16:50
我是RoR和RSpec的新手,我遇到了类似的错误,启动这个线程的伙伴:
故障:
我试过view.pluralize但没走运。最后,我在集成测试中使用了以下代码:
it "should show the total feeds" do
expect(page).to have_content("micropost".pluralize(user.feed.count))
end而且我的测试运行得很好。
希望这能帮助到其他人。
iVieL。
https://stackoverflow.com/questions/14236438
复制相似问题