Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 RSpec未定义方法`全名';零级:零级_Ruby On Rails 4_Rspec_Factory Bot - Fatal编程技术网

Ruby on rails 4 RSpec未定义方法`全名';零级:零级

Ruby on rails 4 RSpec未定义方法`全名';零级:零级,ruby-on-rails-4,rspec,factory-bot,Ruby On Rails 4,Rspec,Factory Bot,我试图用RSpec和FactoryGirl测试以下内容: describe 'GET #show' do it "assigns the requested dish to @dishes" do dish = create(:dish) get :show, id: dish expect(assigns(:dish)).to eq dish end end 但我得到了以下错误: 1) DishesController G

我试图用RSpec和FactoryGirl测试以下内容:

describe 'GET #show' do
    it "assigns the requested dish to @dishes" do
        dish = create(:dish)
        get :show, id: dish
        expect(assigns(:dish)).to eq dish
    end
end

但我得到了以下错误:

  1) DishesController GET #show assigns the requested dish to @dishes
     Failure/Error: get :show, id: dish
     ActionView::Template::Error:
       undefined method `full_name' for nil:NilClass
     # ./app/views/dishes/show.html.erb:3:in `_app_views_dishes_show_html_erb___4424609358762382481_2199208220'
     # ./app/controllers/dishes_controller.rb:16:in `block (2 levels) in show'
     # ./app/controllers/dishes_controller.rb:15:in `show'
     # ./spec/controllers/dishes_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
my dish.rb model
属于:author
,my author.rb model
有许多:dish

我试着用谷歌搜索了一下,但似乎找不到任何关于我的问题的具体答案,除了这个帖子,但我真的不明白那里发生了什么

如果需要更多信息,请告诉我

提前谢谢
Chris

您得到了未定义的nil:NilClass方法“full_name”的
错误,因为在
@dish.author.full_name
中,
@dish.author
nil,即dish记录没有匹配的author记录

您需要的是设置工厂,以便在创建盘时也会创建相关记录:

FactoryGirl.define do
  factory :dish, :class => 'Dish' do |f|
    f.name "TestDish"
    f.header "TestHeader"
    f.avatar { File.open("spec/support/sample_photo.jpg")}
    ## f.author_id "1"  Remove this line
    association :author, factory: :assoc_author  ## set association
  end

  factory :assoc_author, :class => 'Author' do |f|
    ## assign fields
    f.first_name "John"
    f.last_name "Doe" 
  end
  ## ...
end

这样,当您调用
create(:dish)
时,它将在
disks
表中创建一条记录,并在
authors
表中创建一条相关记录

我是否可以配置我的测试,使一个盘子有一个匹配的作者呢?这完全有道理。。。但现在我得到一个:
方法\u缺失”:工厂已经注册:author
错误。大概是因为我已经有了一个作家工厂?是的,没错。再次查看我的更新。我更改了工厂名称,使其与现有工厂不冲突。或者,您可以使用现有工厂。不要包括这个新的。只需确保在
factory:dish
中设置关联(使用正确的现有工厂名称)。恐怕无法解决此问题。它仍然返回相同的错误。这可能和关系有关吗?
def full_name
        full_name = first_name + " " + last_name
    end
FactoryGirl.define do
  factory :dish, :class => 'Dish' do |f|
    f.name "TestDish"
    f.header "TestHeader"
    f.avatar { File.open("spec/support/sample_photo.jpg")}
    ## f.author_id "1"  Remove this line
    association :author, factory: :assoc_author  ## set association
  end

  factory :assoc_author, :class => 'Author' do |f|
    ## assign fields
    f.first_name "John"
    f.last_name "Doe" 
  end
  ## ...
end