A
A
Alexander Gonchurin2014-09-09 17:23:52
Ruby on Rails
Alexander Gonchurin, 2014-09-09 17:23:52

How does this code from "RoR Tutorial" (Rspec) work?

Explain how the code works ( railstutorial.ru/chapters/4_0/filling-in-the-layyou...
Here it is:

require 'spec_helper'
describe ApplicationHelper do
describe "full_title" do
it "should include the page title" do
expect(full_title("foo")).to match(/foo/)
end
it "should include the base title" do
expect( full_title("foo")).to match(/^Ruby on Rails Tutorial Sample App/)
end
it "should not include a bar for the home page" do
expect(full_title("")).not_to match(/\| /)
end
end
end

What is the string "foo" and where did we get it from?
What is the advantage of this code compared to the following:
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2014-09-09
@imalexgo

omg
1) The string foo is a parameter that you pass to the full_title function, expecting that in this case the result will return something that will match the regexp with foo
2) There is absolutely no advantage. It's just that the first code is the code from the test suite, the second a is the definition of the method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question