dimanche 17 mai 2015

How to chain allow_any_instance_of with Rspec

I'm in a bit of trouble in learning how rspec stub works.

I have to test the following helper method that I wish to test the output string as html:

def build_links(resource)
  YAML.load_file("config/admin_links.yml")[resource].collect do |link|
  active = (eval(link.last) == request.path) ? "active" : ""
  path = link.last.empty? ? "#" : eval(link.last)

  content_tag(:li, link_to(link.first, path), class: active)
end.join.html_safe

And I want to test if the method is building the links in the right way. I can't discover how to do the stubs for the YAML.load_file("config/admin_links.yml")[resource] to output an Hash for the collect method to work on.

I have already tested using

hash_link = {"Dashboard"=>"admin_dashboard_path", "Configurações"=>""}
allow_any_instance_of(YAML).to receive(:load_file).with(["test"]).and_return(hash_link)

u = YAML
allow(u).to receive_message_chain(:load_file, :[] ,:collect)

But no success so far. How i'm supposed to do the mock so "YAML.load_file("config/admin_links.yml")[resource]" returns "{"Dashboard"=>"admin_dashboard_path", "Configurações"=>""" so the method collect can work on the variable?

tnx,

Aucun commentaire:

Enregistrer un commentaire