ActionView::Template::Error: undefined method `image_url' for nil:NilClass when doing integration test with rspec rails
ActionView::Template::Error: undefined method `image_url' for nil:NilClass when doing integration test with rspec rails
我是 rails 的初学者,我正在尝试测试我的 rails 应用程序
我的页面工作正常,但在进行集成测试时,出现此错误
User signs in with username
Failure/Error: <div class="art-img" style="background: url(<%= @article.image_url %>); background-size: 100% 100%; background-repeat: no-repeat; background-position: center">
ActionView::Template::Error:
undefined method `image_url' for nil:NilClass
这是错误的视图模板 app/view/home/index
<div class="main-art-con">
<div class="art-img" style="background: url(<%= @article.image_url %>); background-size: 100% 100%; background-repeat: no-repeat; background-position: center">
<div class="content-con">
<h5 class="art-head"><%= @article.title %></h5>
<p class="art-con">
<%= truncate(@article.text, length: 100) %>
</p>
</div>
</div>
</div>
这是我的包含对象实例的家庭控制器
class HomeController < ApplicationController
def index
@featured_article = Article.unscoped.order(cached_weighted_total: :desc).limit(1)
@article = @featured_article.last
@categories = Category.all.ordered_by_priority
end
end
测试代码
require 'rails_helper'
feature 'User signs in' do
background do
User.create(name: 'Jane Doe', username: 'jodi')
end
scenario 'with username' do
visit login_path
fill_in 'Username', with: 'jodi'
click_on 'Log in'
expect(page).to have_content 'Log Out'
end
end
错误消息 undefined method `image_url' for nil:NilClass
似乎表明 @article
在 test 环境中是 nil
。此环境不同于 development 环境,开发环境及其 class 对象大概就是您在浏览器中看到的。
我希望您可以通过在 运行 测试之前 在测试环境 中创建(至少)一篇文章来解决您的问题:
background do
User.create(name: 'Jane Doe', username: 'jodi')
Article.create(title: 'Your Title', etc...)
end
然后这篇文章将在 test 数据库中持久化,就像用户 Jane Doe 持久化一样。
顺便说一句,您的功能测试似乎也在隐式测试(至少)索引的两个功能:是否存在与文章关联的图像,以及是否存在注销按钮。如果可能,请考虑将这两个测试分开,或者至少对两者进行显式测试。
我是 rails 的初学者,我正在尝试测试我的 rails 应用程序
我的页面工作正常,但在进行集成测试时,出现此错误
User signs in with username
Failure/Error: <div class="art-img" style="background: url(<%= @article.image_url %>); background-size: 100% 100%; background-repeat: no-repeat; background-position: center">
ActionView::Template::Error:
undefined method `image_url' for nil:NilClass
这是错误的视图模板 app/view/home/index
<div class="main-art-con">
<div class="art-img" style="background: url(<%= @article.image_url %>); background-size: 100% 100%; background-repeat: no-repeat; background-position: center">
<div class="content-con">
<h5 class="art-head"><%= @article.title %></h5>
<p class="art-con">
<%= truncate(@article.text, length: 100) %>
</p>
</div>
</div>
</div>
这是我的包含对象实例的家庭控制器
class HomeController < ApplicationController
def index
@featured_article = Article.unscoped.order(cached_weighted_total: :desc).limit(1)
@article = @featured_article.last
@categories = Category.all.ordered_by_priority
end
end
测试代码
require 'rails_helper'
feature 'User signs in' do
background do
User.create(name: 'Jane Doe', username: 'jodi')
end
scenario 'with username' do
visit login_path
fill_in 'Username', with: 'jodi'
click_on 'Log in'
expect(page).to have_content 'Log Out'
end
end
错误消息 undefined method `image_url' for nil:NilClass
似乎表明 @article
在 test 环境中是 nil
。此环境不同于 development 环境,开发环境及其 class 对象大概就是您在浏览器中看到的。
我希望您可以通过在 运行 测试之前 在测试环境 中创建(至少)一篇文章来解决您的问题:
background do
User.create(name: 'Jane Doe', username: 'jodi')
Article.create(title: 'Your Title', etc...)
end
然后这篇文章将在 test 数据库中持久化,就像用户 Jane Doe 持久化一样。
顺便说一句,您的功能测试似乎也在隐式测试(至少)索引的两个功能:是否存在与文章关联的图像,以及是否存在注销按钮。如果可能,请考虑将这两个测试分开,或者至少对两者进行显式测试。