English 中文(简体)
探矿和铁路主计长
原标题:Rspec and Rails Controller Expectations

所有人,

有一些问题值得检验:

it "does something" do
  controller.should_receive(:some_method).once

  expect {
    post :create, some_params_hash, some_session_hash
  }.to change(Something, :count).by(1)
end

铁路方面的主计长——粗略例子:

class SomethingsController
  before_filter :some_method

  def create
    respond_with Something.create params[:something]
  end

  def some_method
    puts  some_method 
  end
end

这既良好又良好,而且只做罚款if。 我删除了控制人员,应当——接收期望。 如果我放弃了期望——测试失败。

令人奇怪的是,它并没有辜负不满意的期望——它实际上似乎满足了人们的期待——它只是说,这一记录造成并随后进行的变革评价失败了。

因此,问题:

这一适当方式是明确作为这一检验一部分而援引的对控制者的期望吗?

感谢任何帮助!

最佳回答

一种常见的光谱错误是,像您的<代码>所设定的方法”一样,一种方法预期会——可能——接收<>/代码”只是监测申请,以确保某些事情发生。 但是实际上,它完全将自己插入到流通和replaces

因此,你的控制器的<代码> 有些_method将代之以只有零的回报。 事先过滤器将停止所有处理。 你们的行动永远不会被人们所呼吁。

改变你们的期望:

controller.should_receive(:some_method).once.and_return true

又注意到你的例子正在检验两件事——它确保你的行动电话some_method,并且确保坚持的几条字数增加一个。 如果你真的只打算检查后者,那么你就可以使用一种 st,而不是一种期望,而这种期望更小:

controller.stub some_method: true

<><>UPDATE: 我还要指出,在最近的铁路中,控制器过滤器的回报价值被忽视。 (过滤器可防止行动仅仅通过提供一些东西而付诸执行。) 然而,更换贵方位的方法的仪表 should_receive原则仍然有效,而且普遍适用。

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签