我正在嘗試為應用程式撰寫測驗,該應用程式使用載波進行影像上傳。該應用程式正在運行。我只是從非常簡單的控制器測驗開始。
例如,當嘗試通過
test "should get root" do
get root_path
assert_response :success
end
我收到以下錯誤訊息:
ERROR PagesControllerTest#test_should_get_root (1.52s)
Minitest::UnexpectedError: ActionView::Template::Error: undefined method `file_name' for nil:NilClass
app/views/pages/manifesto.html.erb:1
test/controllers/pages_controller_test.rb:6:in `block in <class:PagesControllerTest>'
令人不快的觀點是:
<div class="" style="background: url(<%= @hero.file_name.url(:image2000) %>) no-repeat center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;">
</div>
我有以下相關的裝置:
page_sections.yml
one:
title: MyString
page: MyString
description: MyString
two:
title: MyString
page: MyString
description: MyString
...
page_section_images.yml
one:
page_section: One
image: One
two:
page_section: Two
image: Two
...
images.yml
one:
file_name: MyString
title: MyString
alt: MyString
author: MyString
copyright: MyString
two:
file_name: MyString
title: MyString
alt: MyString
author: MyString
copyright: MyString
我知道他在嘗試渲染視圖時沒有獲得背景影像。為什么呢?為了能夠測驗我的觀點,我必須做什么?我目前沒有測驗上傳。正如我所說的頁面正確呈現,只有測驗會拋出這個錯誤。
我希望有人可以幫助我。
編輯/ pages_controller:
class PagesController < ApplicationController
def manifesto
@intro_hero = Image.joins(:page_section_images).where('page_section_images.page_section_id' => '1').order("RAND()").uniq.first
@intro_section = PageSection.where(id: 1).select("id, description, title").first
@hero = Image.joins(:page_section_images).where('page_section_images.page_section_id' => '2').order("RAND()").uniq.first
@section = PageSection.where(id: 2).select("id, description, title").first
...
end
...
end
在我的應用程式中,我有 11 個頁面部分,因此我在 page_section 固定裝置 11 條目和 page_section_images 固定裝置 11 條目中進行了定義。我是撰寫測驗和設定測驗的新手。
uj5u.com熱心網友回復:
感謝 Christos-Angelos Vasilopoulos 的提示,我能夠解決它。問題與我的燈具中的正確資料有關。
在我的情況下,一些控制器和模型具有與我的資料庫中的精確條目相關的邏輯。例如“where('page_section_images.page_section_id' => '2')”之類的東西。在撰寫固定裝置時,我必須在代碼中使用我需要的整數值來指定 id。否則 Minitest 會寫出他的值,而我的代碼不會接受這些值。
我有這樣的代碼:".order("RAND()").uniq.first"。為了使其可測驗,我需要至少兩個可以排序、隨機化等的條目......
這意味著需要徹底撰寫固定裝置并牢記代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483826.html
