Is it possible to test the pluralize function in 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>
ANSWER: (as pointed by Eric C below)
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
The answer to your question is… sort of. Rails provides the functionality to use pluralize and using rspec-rails, assuming that’s what you are using, gives you the ability to call on rails methods as you need them. If this is a view test, which is sort of what it looks like, you can type something like:
The key here is that you’re adding the view. before pluralize.
I would like to stress that when testing, it’s not a great idea to test a helper method inside what appears to be a view test. If you’d really like to test pluralize itself, it’d be better to test it in a helper spec. Doing something like:
This way you can be assured of the outcome and make assumptions in other tests and then test the proper outcome. It actually will inevitably make the tests better, because if a helper like pluralize changes then you have two tests that alarm of this change. You then can adjust accordingly. Just a thought.