English 中文(简体)
• 如何验证仪表模版的当地情况
原标题:How to validate locals of render template in rspec

我想知道,如何验证当地人为控制器提供模版而通过的地方。

主计长:

def lelf_panel
  # ...
  if some_condition
    locals_hash = some_very_long_hash_A
  else
    locals_hash = some_very_long_hash_B
  end
  render :partial => "left_panel", :layout => false, :locals => locals_hash
end

现有光谱:

it  should render correct template for lelf_panel  do
  # ... 
  get  left_panel 
  response.should render_template( system/_left_panel )
end   

现在,我需要为这一控制员完成Rcov的工作,因此,我需要增加/修改光谱,以涵盖某些特定结果。 我愿证实,“Elf_panel”的当地人转来了,因为如果我只证实这一错误,那么这两种结果的部分页就相同。

I check the render_template in rspec docs in http://rubydoc.info/gems/rspec-rails/2.8.1/RSpec/Rails/Matchers/RenderTemplate:render_template

它只是提供信息的第2段,因此,我如何检验当地人传递的信息?

最佳回答

我知道,没有办法直接审查当地人如何重新描述模板。

你们可以把当地人换成@ locals_hash,然后通过分配来审查结果(:当地人_hash)。

或者,如果当地人对网页名称有影响,则可以就由此产生的超文本使用挑选器,并检查某些指示性内容——例如,如果当地人对网页名称有影响,则检查由此产生的超文本页标题是你预期的。

问题回答

您不使用<代码>render_template匹配器,而是可以使用对控制器标的的预期。

it  should render correct template for lefl_panel  do
  # ...
  allow(controller).to receive(:render).with no_args
  expect(controller).to receive(:render).with({
    :partial =>  system/_left_panel ,
    :layout  => false,
    :locals  => some_very_long_hash_A
  })
  get  left_panel 
end

Same as @ryan-ahearn s response with recommendations from @user2490003 s comment - but all put into some more Flex and for RSpec 3.

  # Safe to set globally, since actions can either render or redirect once or fail anyway
  before do
    allow(controller).to receive(:render).and_call_original
  end

  describe "get left panel" do
    before do
      # other setup
      get  left_panel 
    end

    it  should render correct template for lelf_panel  do
      # Sadly, render_template is primitive (no hash_including, no block with args, etc.)
      expect(subject).to render_template( system/_left_panel )
    end

    it  should render with correct local value  do
      expect(controller).to have_received(:render) do |options|
        expect(options[:locals][:key_from_very_long_hash]).to eq( value_of_key_from_very_long_hash )
      end
    end
  end




相关问题
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: ...

热门标签