Rails Tutorial Chap 5 Exercise 3 - What did the original full_title function do?

I am confused by Chap 5 Exercise 3 here , which replaces the need for a full_title validation assistant

Specification / Support / utilities.rb:

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 

There is also a helper function rails with the same name:

 module ApplicationHelper # Returns the full title on a per-page basis. 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 end 

by creating an application helper that directly checks the function: specifications / helpers / application_helper_spec.rb

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

It's great that he tests the rails helpers function directly, but I thought the full title function in the .rb utility is for use in Rspec code. Therefore, how can we eliminate the above code in utilities.rb and replace it simply:

 include ApplicationHelper 

I made a swap and it still worked. I was expecting Rspec code, which, although I used the rspec function as shown below, but it is not:

 it "should have the right links on the layout" do visit root_path click_link "About" page.should have_selector 'title', text: full_title('About Us') ... 

Is the above function call always called with a pointer to the actual rails function, and not with the respec function? If I could eliminate it, what was it first for? I feel that something is missing here. Thanks for any help. It seems like a bad idea to make changes, I don’t understand when my goal is to learn Rails.

Thanks Mark

+4
source share
2 answers

full_title in specifications is always calls from spec / support / utilities.rb.

Before you replaced the include ApplicationHelper code, full_title in the specifications called the function found in utilities.rb:

 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 

Replacing the code simply

 include ApplicationHelper 

to be clear, you actually include

 module ApplicationHelper 

from assistants / application _helper.rb.

This parameter has nothing26> in spec / helpers / application_helper_spec.rb

What really happens is that the full_title function from module ApplicationHelper now mixed in (see Mixins) on utilities.rb. Therefore, utilities.rb accesses the full_title function from module ApplicationHelper (helpers / application_helper.rb).

So, when specs calls the full_title function, it is called from utilities.rb, which is possible because the function was mixed using include ApplicationHelper .

+4
source

Unfortunately, the exercise says:

Eliminate the need for the full_title check assistant in Listing 5.29 to write tests for the original helper method, as shown in Listing 5.41. (You will need to create both the spec / helpers directory and the application_helper_spec.rb file.) Then include it in the test using the code in Listing 5.42.

... which is completely wrong.

Some prerequisites:

The full_title () method in / helpers / application _helper.rb is a method that is available for regular code throughout the application, and not for rspec tests. The full_title () method in spec / support / utilities.rb has been added so that rspec tests can call it.

Terminology:

application helper = full_title method in application / helpers / application _helper.rb

rspec helper = full_title method in spec / support / utilities.rb

Writing tests for a helper application does not eliminate the need for an rspec helper - because writing a test for one method does not magically eliminate the need for any other method. What the text should say looks something like this:

You can eliminate the need for the rspec helper, which is a duplicate of the application helper by replacing rspec with the following statement:

 include ApplicationHelper 

Currently, you can pretend that the inclusion of a module inserts methods into the module at the point of the include statement. The application helper is defined inside a module called ApplicationHelper - open the file app / helpers / application_helper.rb and see.

Then write down the tests for the application assistant to make sure that it works correctly.

0
source

All Articles