为什么我的 Rspec 帖子:创建无效参数控制器测试失败? [英] Why does my Rspec post :create invalid parameter controller test fail?

查看:38
本文介绍了为什么我的 Rspec 帖子:创建无效参数控制器测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器有两个测试.

spec/controllers/employees_controller_spec.rb它应该创建一个新的有效员工"做员工参数 = FactoryGirl.attributes_for(:employee)期望{ post :create, 员工:employee_params }.to change(Employee, :count).by(1)应该重定向到(动作::索引)结尾它不应该保存具有无效头像扩展名的员工"invalid_employee_params = FactoryGirl.attributes_for(:employee)invalid_employee_params[:avatar] = Rack::Test::UploadedFile.new(File.open(File.join(Rails.root, '/spec/fixtures/photos/duck.txt')))期望 { 发布:创建,员工:invalid_employee_params }.to_not change(Employee, :count).by(1)结尾

当我执行这些测试时,第一个测试通过而第二个测试失败.这是下面的错误:

失败/错误:expect { post :create, employee: invalid_employee_params }.to_not change(Employee, :count).by(1)未实现错误:不支持`expect { }.not_to change { }.by()`# ./spec/controllers/employees_controller_spec.rb:38:in `block (3 levels) in <top (required)>'

我遵循了此处找到的 Rspec 文档 Rspec doc 对我来说似乎是正确的.这看起来是一个需要修复的简单错误,但我不太确定发生了什么.有人可以帮忙指出我的错误.我是 Rspec 的新手.

解决方案

很好看的错误,看起来你不能使用 by 匹配器来测试 to_not change.直觉上,您应该只是期望它不会改变计数.你不应该测试它改变 1,因为如果计数改变0或2,该测试就会通过,我想这会导致许多误报>

这行得通吗?

expect { post :create, employee: invalid_employee_params }.to_not change(Employee, :count)

I have two tests for my controllers.

spec/controllers/employees_controller_spec.rb

  it 'should create a new valid employee' do
    employee_params = FactoryGirl.attributes_for(:employee)
    expect { post :create, employee: employee_params }.to change(Employee, :count).by(1)
    should redirect_to(action: :index)
  end



  it 'should not save an employee with an invalid avatar extention' do
    invalid_employee_params = FactoryGirl.attributes_for(:employee)
    invalid_employee_params[:avatar] = Rack::Test::UploadedFile.new(File.open(File.join(Rails.root, '/spec/fixtures/photos/duck.txt')))
    expect { post :create, employee: invalid_employee_params }.to_not change(Employee, :count).by(1)
  end

When I execute these tests, the first test passes while the second one fails. Here is the error below:

Failure/Error: expect { post :create, employee: invalid_employee_params }.to_not change(Employee, :count).by(1)

 NotImplementedError:
   `expect { }.not_to change { }.by()` is not supported
 # ./spec/controllers/employees_controller_spec.rb:38:in `block (3 levels) in <top (required)>'

I have followed Rspec's documentation found here Rspec docand it seems correct to me. This looks like a simple error to fix but I'm not quite sure what is going on. Could someone help point out my error. I am new to Rspec.

解决方案

well looking at the error, it looks like you cant use the by matcher for testing to_not change. intuitively, you should just be expecting it to not change the count. you shouldnt be testing that its not changing by 1, because that test would pass if the count changed by 0 or 2, which i imagine would lead to many false positives

does this work?

expect { post :create, employee: invalid_employee_params }.to_not change(Employee, :count)

这篇关于为什么我的 Rspec 帖子:创建无效参数控制器测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆