Ruby on Rails 教程(第4版)FILL_IN 参考答案

Mac环境

Rails 5.1.4

ruby 2.4.1

bootstrap v3.3.7

 

本人是大叔级别,年轻的时候是用asp来给别人做点小项目,但是半路转行做货车司机,到目前还是专职的货车司机。

最近比较闲,把空闲的macbook拿出来学习新的语言;经过多处了解,选择了当地没有一个招聘职位的语言来学,你们说我是不是闲的蛋疼?

在网上找了很多资源来看,但是觉得没有系统的学习是没啥用处,后来去叉宝找了一下纸版的书,然后就找到了这本,如下图:

 

此书是刚刚出的,我第一时间就买了,叉宝上写着大大的“正版”,然后我就买了。

接下来就是认真的读书了,读着读着,书上居然写着有电子档,我上网打开一看,额,我不想说了。

额,废话不多说了,直接入正题。整本书我刚刚用了一个月读完,并且把书里的项目都做下来了,在做书上的项目过程中,发现很多FILL_IN,刚开始还不明白,撸了很久才发现这个FILL_IN是要我改成相应的code,书上并没有答案列出来,当然,我可能不知道在哪里,书上指一个网址有答案,但我进去后一脸蒙逼的点了X,算了,靠自己来想答案。

以下答案基本是我自己想出来的,并不是标准答案,大家如果发现有问题可以评论中提出,我可以做个test,我直接把部份代码贴出来,【XXOO】的就是FILL_IN处,我直接写成答案了,你们使用时记得删除掉【】这个东东。

 

 

书本94页(第3章  代码清单 3.42:测试根路由 GREEN)

test "should get root" do
    get 【root_url】
    assert_response 【:success】
  end

  

书本112页(第4章  代码清单 4.10:测试回文的方法)

>> def palindrome_tester(s)
>>   if 【s == s.reverse】
>>     puts "It's a palindrome!"
>>   else
>>     puts "It's not a palindrome."
>>   end
>> end

  

书本166页(第5章  代码清单 5.37:直接测试 full_title 辅助方法)

class ApplicationHelperTest < ActionView::TestCase
  test "full title helper" do
    assert_equal full_title,         【"Ruby on Rails"】
    assert_equal full_title("Help"), 【"Help | Ruby on Rails"】
  end

  

书本248页(第7章  闪现消息测试的模板)

follow_redirect!
    assert_template 'users/show'
    assert_not flash.【empty?】

  

 书本309页(第9章  代码清单 9.28:改进后的“记住我”复选框测试模板)(我整个项目做完后再来加这行code出现bug,我在网上找到的【答案】,供大家参考。)

test "login with remembering" do
    log_in_as(@user, remember_me: '1')
    assert_equal 【cookies['remember_token']】, assigns(:user).【remember_token】
  end

  

书本354页(第10章  代码清单 10.56:测试禁止修改 admin 属性)

test "should not allow the admin attribute to be edited via the web" do
    log_in_as(@other_user)
    assert_not @other_user.admin?
    patch user_path(@other_user), params: {
                                    user: { password: 【@other_user.password】,
                                            password_confirmation: 【@other_user.password】,
                                            admin: 【true】} }
    assert_not 【@other_user】.admin?
  end

  

书本390页(第11章  代码清单 11.39:使用 update_columns 的代码模板)

# 激活账户
  def activate
    update_columns(activated: 【true】, activated_at: 【Time.zone.now】)
  end

  

书本391页(第11章  代码清单 11.39:使用 update_columns 的代码模板)

def index
    @users = User.where(activated: 【true】).paginate(page: params[:page])
  end

 

def show
    @user = User.find(params[:id])
    redirect_to root_url and return unless 【@user.activated】
  end

  

书本419页(第12章  代码清单 12.20:使用 update_columns 的代码模板)

def create_reset_digest
    self.reset_token = User.new_token
    update_columns(reset_digest: User.digest【(reset_token)】, reset_sent_at: 【Time.zone.now】)
  end

  

书本420页(第12章  代码清单 12.21:测试密码重设请求超时)

# 密码重设请求超时
  test "expired token" do
    get new_password_reset_path
    post password_resets_path, params: { password_reset: { email: @user.email } }

    @user = assigns(:user)
    @user.update_attribute(:reset_sent_at, 2.hours.ago)
    patch password_reset_path(@user.reset_token),
          params: { email: @user.email,
                    user: { password: "foobar",
                            password_confirmation: "foobar" } }
    assert_response :redirect
    follow_redirect!
    # 进行全文匹配,任何正则表达式元字符都不会被转义
    assert_match /【Password reset has expired.】/i, response.body
  end

  

书本468页(第13章  代码清单 13.57:侧边栏中微博数量的测试模板)(第一个FILL_IN {34} 取决于你当前有多少条微博就写多少条)

test "micropost sidebar count" do
    log_in_as(@user)
    get root_path
    assert_match "【34 microposts】", response.body
    # 这个用户没有发布微博
    other_user = users(:malory)
    log_in_as(other_user)
    get root_path
    assert_match "0 microposts", response.body
    other_user.microposts.create!(content: "A micropost")
    get root_path
    assert_match "【1 micropost】", response.body
  end

  

书本473页(第13章  代码清单 13.63:测试图像上传功能的模板)(这里的第三个FILL_IN不是我想出来的,我在【这里】找到答案)

test "micropost interface" do
    log_in_as(@user)
    get root_path
    assert_select 'div.pagination'
    assert_select 'input[type=【file】]'
    # 无效提交
    assert_no_difference 'Micropost.count' do
      post microposts_path, params: { micropost: { content: "" } }
    end
    assert_select 'div#error_explanation'
    # 有效提交
    content = "This micropost really ties the room together"
    picture = fixture_file_upload('test/fixtures/timg.jpeg', 'image/jpeg')
    assert_difference 'Micropost.count', 1 do
      post microposts_path, params: { micropost: { content: content, picture: 【picture】 } }
    end
    assert 【assigns(:micropost)】.picture?
    follow_redirect!
    assert_match content, response.body
    # 删除一篇微博
    assert_select 'a', text: 'delete'
    first_micropost = @user.microposts.paginate(page: 1).first
    assert_difference 'Micropost.count', -1 do
      delete micropost_path(first_micropost)
    end
    # 访问另一个用户的资料页面(没有删除链接)
    get user_path(users(:archer))
    assert_select 'a', text: 'delete', count: 0
  end

  

书本529页(第14章  代码清单 14.49:测试动态流的 HTML)

test "feed on Home page" do
    get root_path
    @user.feed.paginate(page: 1).each do |micropost|
      assert_match CGI.escapeHTML(【"\n"】), 【response.body】
    end

 

完!

 

posted @ 2017-11-20 00:15  roy0524  阅读(762)  评论(1编辑  收藏  举报