为什么我的Rspec发布:创建无效的参数控制器测试失败?

时间:2017-01-18 21:36:16

标签: ruby-on-rails ruby rspec

我的控制器有两个测试。

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

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

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)>'

我已经按照此处Rspec doc找到了Rspec的文档,这对我来说似乎是正确的。这看起来像是一个简单的错误修复,但我不太确定发生了什么。有人可以帮助指出我的错误。我是Rspec的新手。

1 个答案:

答案 0 :(得分:0)

仔细查看错误,您似乎无法使用componentWillReceiveProps (props){ this.doSomething(props) // To some function. this.setState({data: props}) // This will update your component. } 匹配器来测试by。直观地说,你应该期望它不会改变计数。您不应该测试其更改 1,因为如果计数更改为0或2,该测试将通过,我想这会导致许多误报

这有用吗?

to_not change