在我的Rails项目中,我使用rspec mock来使用任何_实例,但我想避免这种不赞成的消息:
Using any_instance from rspec-mocks' old :should syntax without explicitly enabling the syntax is deprecated. Use the new :expect syntax or explicitly enable :should instead.
以下是我的规格:
describe (".create") do
it 'should return error when...' do
User.any_instance.stub(:save).and_return(false)
post :create, user: {name: "foo", surname: "bar"}, format: :json
expect(response.status).to eq(422)
end
end
这是我的控制器:
def create
@user = User.create(user_params)
if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
我想用new :expect syntax,但我找不到如何正确使用它.
我用的是RSpec 3.0.2.