Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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 如何检查是否使用RSpec调用了inside方法_Ruby On Rails_Ruby_Unit Testing_Ruby On Rails 3_Rspec - Fatal编程技术网

Ruby on rails 如何检查是否使用RSpec调用了inside方法

Ruby on rails 如何检查是否使用RSpec调用了inside方法,ruby-on-rails,ruby,unit-testing,ruby-on-rails-3,rspec,Ruby On Rails,Ruby,Unit Testing,Ruby On Rails 3,Rspec,我获得了一个通过id更新此人的方法: def update_person(id) handle_exceptions do person = Person.find(id) #...other end end 当这个id不存在时,应该调用handle_异常。但是我怎样才能测试它呢?我写的测试是: context 'not found the proposals' do subject {controller.send(:update_person, 3)}

我获得了一个通过id更新此人的方法:

def update_person(id)
  handle_exceptions do
     person = Person.find(id)
     #...other
  end
end
当这个id不存在时,应该调用handle_异常。但是我怎样才能测试它呢?我写的测试是:

context 'not found the proposals' do
  subject {controller.send(:update_person, 3)}

  before do
    allow(Person).to receive(:find).and_raise(ActiveRecord::RecordNotFound)
    allow(subject).to receive(:handle_exceptions)
  end

  it 'calls handle_exceptions' do
    expect(subject).to have_received(:handle_exceptions)
  end
end
但它不起作用,我失败了,我说: 失败/错误:预期(主题)。接收到(:处理异常)


handle\u exceptions
方法是

def handle_exceptions
  yield
rescue ActiveRecord::RecordNotFound => e
  flash[:warning] = 'no record found'
  Rails.logger.error message: e.message, exception: e
  @error_data = { message: 'no record found', status: :not_found }
end

问题是您正在主题块中调用被测试的方法

subject {controller.send(:update_person, 3)}
这实际上是在示例运行之前和
before
块之前调用的

context 'not found the proposals' do
  before do
    allow(subject).to receive(:handle_exceptions)
  end

  it 'calls handle_exceptions' do
    controller.send(:update_person, "NOT A VALID ID")
    expect(subject).to have_received(:handle_exceptions)
  end
end
但就测试而言,这一次并不好。您正在测试
update\u person
的实现,而不是实际行为。您正在使用
update\u person.send(:update\u person,3)
调用该方法


相反,您应该测试控制器在尝试使用无效id进行更新时是否返回404响应代码。这也是为什么您坚持存根
Person。查找
是一个谜,因为您只需传递无效id即可触发异常。只有在实际需要时才可以存根。

问题是您在下面调用该方法在主题块中进行测试

subject {controller.send(:update_person, 3)}
这实际上是在示例运行之前和
before
块之前调用的

context 'not found the proposals' do
  before do
    allow(subject).to receive(:handle_exceptions)
  end

  it 'calls handle_exceptions' do
    controller.send(:update_person, "NOT A VALID ID")
    expect(subject).to have_received(:handle_exceptions)
  end
end
但就测试而言,这一次并不好。您正在测试
update\u person
的实现,而不是实际行为。您正在使用
update\u person.send(:update\u person,3)
调用该方法


相反,您应该测试控制器在尝试使用无效id进行更新时是否返回404响应代码。以及为什么您坚持存根
个人。查找
是一个谜,因为您只需传递无效id即可触发异常。只有在实际必须时才存根。

工作几天后,我意识到我对它感到困惑的原因是我没有弄清楚“谁调用了这个函数”,我认为在测试它之前知道它是最重要的。对于这样的方法:

class User::Controller
  def methodA
      methodB 
  end

  def methodB
     // ...
  end

我犯的错误是我认为methodB是由方法调用的,但事实并非如此。它是由控制器调用的,这就是我无法使测试正常工作的原因。有那么多东西需要学习,我希望有一天我不会犯这样的错误,能够帮助别人。

经过几天的工作,我意识到我对它感到困惑的原因是我没有弄清楚“谁调用了这个函数”,我认为在测试它之前知道它是最重要的。对于这样的方法:

class User::Controller
  def methodA
      methodB 
  end

  def methodB
     // ...
  end

我犯的错误是我认为methodB是由方法调用的,但事实并非如此。它是由控制器调用的,这就是我无法使测试正常工作的原因。有很多东西需要学习,我希望有一天我不会犯这样的错误,能够帮助别人。

总是调用
处理异常的方法。它包装了数据库查找。但是,您能显示
handle\u exceptions
方法的内容吗?方法定义是什么?你在模仿allow(subject)。to receive(:handle_exceptions),所以我不明白为什么人应该receive find。@maxpleaner嗨,max,我已经在我的问题中添加了
handle_exceptions
,你似乎真的没有阅读或理解我对一周前你问的同样问题的回答。与其测试调用了
handle\u exceptions
,不如检查是否填充了
@error\u data
,并且可能调用了
Rails.logger.error
,始终调用
handle\u exceptions
方法。它包装了数据库查找。但是,您能显示
handle\u exceptions
方法的内容吗?方法定义是什么?你在模仿allow(subject)。to receive(:handle_exceptions),所以我不明白为什么人应该receive find。@maxpleaner嗨,max,我已经在我的问题中添加了
handle_exceptions
,你似乎真的没有阅读或理解我对一周前你问的同样问题的回答。与其测试调用了
handle\u exceptions
,不如检查是否填充了
@error\u data
,可能还调用了
Rails.logger.error
。谢谢,我确实阅读了您关于我的另一个问题的答案,根据您的建议,我将测试我的控制器是否返回404响应。谢谢,我确实读过你关于我的另一个问题的答案,根据你的建议,我将测试我的控制器是否返回404响应