当我试图告诉 Capybara 时,我有以下失败的测试visit ('/')
。
1) Credits when user is non-enabled show user credits available at the top.
Failure/Error: visit ('/')
NoMethodError:
undefined method `tag?' for nil:NilClass
# ./app/controllers/application_controller.rb:16:in `set_current_tag'
# ./spec/requests/credits_spec.rb:12:in `block (3 levels) in <top (required)>'
中的代码ApplicationController
如下所示
def set_current_site
current_host = request.host
@site = Site.where(domain: current_host).first
@site = Site.where(domain: 'mysiste.com').first unless @site.present?
end
def set_current_tag
if @site.tag? && Dvd.tagged_with(@site.tag, wild: true, any: true).any?
@site.tag = @site.tag
elsif params[:tag] && Dvd.tagged_with(params[:tag], wild: true, any: true).any?
@site.tag = params[:tag]
else
@site.tag = ''
end
end
我在这里有点迷失如何解决这个问题以及什么是理想的解决方案。我是否应该将实例变量的设计更改为方法并同时使用 FactoryGirl for Site?
站点模型非常简单。
class Site < ActiveRecord::Base
attr_accessible :domain, :content, :program, :tag, :locale, :parent, :title, :landing
validates_presence_of :domain
validates_uniqueness_of :domain
end
而且工厂是
FactoryGirl.define do
factory :site do
domain "dvdstore.com"
title "DVD Store"
program "premium"
content "movies"
end
end