trailblazer / rspec-cells

Spec your Cells.
http://cells.rubyforge.org
MIT License
62 stars 48 forks source link

How to test cell with usage of `url_for`? #49

Closed PikachuEXE closed 9 years ago

PikachuEXE commented 10 years ago

It will only gives "No route matches" error

For now I use

url_for(params.merge({only_path: true, locale: locale})) rescue localized_root_path(locale: locale)
apotonick commented 10 years ago

Is that url_forin a cell or in the spec example?

PikachuEXE commented 10 years ago

It's in cell template like desktop.haml

apotonick commented 10 years ago

Ok! And it works in production?

That's weird, I have that all the time and it works..... both in rpsec and dev/prod.

PikachuEXE commented 10 years ago

I tried that in dev, not on production yet

apotonick commented 10 years ago

And in dev the url_for gets rendered, right? Have I mentioned how much I hate Rails' way of "injecting" url helpers?

PikachuEXE commented 10 years ago

Now I start writing helper with extend self without including them Not the best but still better than including everything?

Need to use module_eval and extend self though, sometimes has bug as well with this approach.

apotonick commented 10 years ago

Does your url_for stuff work in the browser, no rspec?

PikachuEXE commented 10 years ago

Yes it does

PikachuEXE commented 10 years ago

Maybe I should stub params?

apotonick commented 10 years ago

Ah, because you didn't set a host and have to specify :only_path.

PikachuEXE commented 10 years ago

I already have :only_path (see top comment) I got it working by stubbing params in spec

before
  allow(controller).to receive(:params) do
    {controller: :home, action: :index}
  end
end
apotonick commented 10 years ago

Ah... I know what is the problem. In a real environment we get this kind of data from parent_controller (passed into SongCell.new(parent_controller, ..). This is missing here apparently.

PikachuEXE commented 10 years ago

I guess the available action here is to add some note about testing for cells using parent_controller (which includes params and other I don't know what).

Edit: Off from work now~

apotonick commented 10 years ago

This will be more straight-forward in Cells 4.0, I want to entirely get rid of the parent_controller dependency, so rspec and dev/prod render calls will be identical.

I still don't understand why params is used in url_for, but this might be some Rails bullshit.

PikachuEXE commented 10 years ago

I use url_for(params.merge({only_path: true, locale: locale})) which contains params :3

PikachuEXE commented 10 years ago

rails 4.2 beta released Start bombing them after the stable 4.2 is released!

tjjjwxzq commented 7 years ago

Is setting the controller dependency using ::controller supposed to fix this? Because I've tried testing some cells with RSpec, where the template is rendering some Kaminari helper which is calling url_for, and it still doesn't seem like the params are filled in properly (self.params called from within the template returns an empty hash), so I'm still stuck with stubbing out #params on controller

apotonick commented 7 years ago

The ::controller method simply creates an empty controller instance. The problem lies in Kaminari, it should never use a global variable like params, but unfortunately, it does.

tjjjwxzq commented 7 years ago

right. I see

akbarbin commented 7 years ago

+1 for adding only_path: true when running testing rspec.