Ruby on rails 4 rspec未定义的方法`更新';零级:零级
大家好,我有一个问题。我正在用rspec对控制器进行测试,当我测试方法删除和更新时,我得到了错误:Ruby on rails 4 rspec未定义的方法`更新';零级:零级,ruby-on-rails-4,factory-bot,rspec-rails,Ruby On Rails 4,Factory Bot,Rspec Rails,大家好,我有一个问题。我正在用rspec对控制器进行测试,当我测试方法删除和更新时,我得到了错误: 1) TarefasController PATCH #update with valid attributes locates the requested @tarefa Failure/Error: if @tarefa.update(tarefa_params) NoMethodError: undefined method `update' for n
1) TarefasController PATCH #update with valid attributes locates the requested @tarefa
Failure/Error: if @tarefa.update(tarefa_params)
NoMethodError:
undefined method `update' for nil:NilClass
# ./app/controllers/tarefas_controller.rb:35:in `update'
# ./spec/controllers/tarefas_controller_spec.rb:82:in `block (4 levels) in <top (required)>'
我的控制器:
class TarefasController < ApplicationController
before_filter :authenticate_user!
def index
@tarefas = current_user.tarefas.all.order(:data)
@data = DateTime.now
end
def show
@tarefa = Tarefa.find(params[:id])
end
def new
@tarefa = Tarefa.new
@data = DateTime.now.strftime("%Y%m%d%H%M")
end
def edit
@tarefa = current_user.tarefas.find_by(id: params[:id])
@data = DateTime.now.strftime("%Y%m%d%H%M")
end
def create
@tarefa = current_user.tarefas.new(tarefa_params)
if @tarefa.save
redirect_to @tarefa
else
render 'new'
end
end
def update
@tarefa = current_user.tarefas.find_by(id: params[:id])
if @tarefa.update(tarefa_params)
redirect_to @tarefa
else
render 'edit'
end
end
def destroy
@tarefa = current_user.tarefas.find_by(id: params[:id])
@tarefa.destroy
redirect_to tarefas_path
end
def complete_index
@tarefas = current_user.tarefas.complete.order(:data)
end
def complete_update
@tarefa = current_user.tarefas.find_by(id: params[:id])
@tarefa.update(complete:true)
@tarefa.update(data_complete:DateTime.now.strftime("%Y%m%d%H%M") )
end
def incomplete_task
@tarefa = current_user.tarefas.find_by(id: params[:id])
if @tarefa.update(data_tarefa_params)
@tarefa.update(complete:false)
redirect_to complete_index_tarefas_path
end
end
def edit_complete_task
@tarefa = current_user.tarefas.find_by(id: params[:id])
@data = DateTime.now.strftime("%Y%m%d%H%M")
end
private
def tarefa_params
params.require(:tarefa).permit(:titulo, :descricao, :data, :complete)
end
def data_tarefa_params
params.require(:tarefa).permit(:data)
end
end
我的规范/工厂/用户.rb
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
password { Faker::Internet.password(8) }
end
end
patch:update,id:@tarefa
=>patch:update,id:@tarefa.id
Hmm。。。我更改了,但在您创建@tarefa=create.时运行测试时,错误消息保持不变
你没有在那里分配用户:当前用户
。我正在使用带有rspec的设备,我想它已经定义了当我调用login\u user时用户登录的方式,我没有进行完整的测试,我正在用完整的测试编辑上面的帖子是,但是你的“tarefa”属于另一个用户。您可以确保是否将find_替换为(id:params[:id])
到find(params[:id])
FactoryGirl.define do
factory :tarefa do
association :user
titulo { Faker::Name.title }
descricao { Faker::Name.title }
data {Faker::Time.between(DateTime.now, DateTime.now + 365.days)}
end
end
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
password { Faker::Internet.password(8) }
end
end