<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 11.6 練習 電子書中有練習的答案,如果想閱讀參考答案,請[購買電子書](http://railstutorial-china.org/#purchase)。 避免練習和正文沖突的方法參見[第 3 章練習](chapter3.html#mostly-static-pages-exercises)中的說明。 1. 重構首頁視圖,把 `if-else` 語句的兩個分支分別放到單獨的局部視圖中。 2. 為側邊欄中的微博數量編寫測試(還要檢查使用了正確的單復數形式)。可以參照[代碼清單 11.67](#listing-sidebar-micropost-count)。 3. 以[代碼清單 11.68](#listing-image-upload-test) 為模板,為 [11.4 節](#micropost-images)的圖片上傳程序編寫測試。測試之前,要在固件文件夾中放一個圖片(例如,可以執行 `cp app/assets/http://railstutorial-china.org/book/images/rails.png test/fixtures/` 命令)。(如果使用 Git,建議你更新 `.gitignore` 文件,如[代碼清單 11.69](#listing-gitignore-uploads) 所示。)為了避免出現難以理解的錯誤,還要配置 CarrierWave,在測試中不調整圖片的尺寸。方法是創建一個初始化腳本,寫入[代碼清單 11.70](#listing-skip-resize-initializer) 中的內容。[代碼清單 11.68](#listing-image-upload-test) 中添加的幾個斷言,檢查首頁有沒有文件上傳字段,以及成功提交表單后有沒有正確設定 `picture` 屬性的值。注意,在測試中上傳固件中的文件使用的是專門的 `fixture_file_upload` 方法。[[17](#fn-17)]提示:為了檢查 `picture` 屬性的值,可以使用 [10.1.4 節](chapter10.html#activation-test-and-refactoring)介紹的 `assigns` 方法,在提交成功后獲取 `create` 動作中的 `@micropost` 變量。 ##### 代碼清單 11.67:側邊欄中微博數量的測試模板 test/integration/microposts_interface_test.rb ``` require 'test_helper' class MicropostInterfaceTest < ActionDispatch::IntegrationTest def setup @user = users(:michael) end . . . test "micropost sidebar count" do log_in_as(@user) get root_path assert_match "#{FILL_IN} 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 FILL_IN, response.body end end ``` ##### 代碼清單 11.68:圖片上傳測試的模板 test/integration/microposts_interface_test.rb ``` require 'test_helper' class MicropostInterfaceTest < ActionDispatch::IntegrationTest def setup @user = users(:michael) end test "micropost interface" do log_in_as(@user) get root_path assert_select 'div.pagination' assert_select 'input[type=FILL_IN]' # 無效提交 post microposts_path, micropost: { content: "" } assert_select 'div#error_explanation' # 有效提交 content = "This micropost really ties the room together" picture = fixture_file_upload('test/fixtures/rails.png', 'image/png') assert_difference 'Micropost.count', 1 do post microposts_path, micropost: { content: content, picture: FILL_IN } end assert FILL_IN.picture? follow_redirect! assert_match content, response.body # 刪除一篇微博 assert_select 'a', '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 . . . end ``` ##### 代碼清單 11.69:在 `.gitignore` 中添加存儲上傳圖片的文件夾 ``` # See https://help.github.com/articles/ignoring-files for more about ignoring # files. # # If you find yourself ignoring temporary files generated by your text editor # or operating system, you probably want to add a global ignore instead: # git config --global core.excludesfile '~/.gitignore_global' # Ignore bundler config. /.bundle # Ignore the default SQLite database. /db/*.sqlite3 /db/*.sqlite3-journal # Ignore all logfiles and tempfiles. /log/*.log /tmp # Ignore Spring files. /spring/*.pid # Ignore uploaded test images. /public/uploads ``` ##### 代碼清單 11.70:一個初始化腳本,在測試中不調整圖片的尺寸 config/initializers/skip_image_resizing.rb ``` if Rails.env.test? CarrierWave.configure do |config| config.enable_processing = false end end ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看