Cucumber fails with "Undefined method render" - ruby-on-rails

I have a link_tag in view which add rendered content to page
link_to("add",nil,:id=>"create_row_cycle",:onclick=>"$('div#cycle_form table').append('#{escape_javascript(render(:partial=>'cycles', :object=>Cycle.new))}');return false;", :href=>"")
I want to test that code in cucumber and cucumber fails that method RENDER undefined.
my step is
find('div#cycle_form table').text.should have_content(render(:partial=>'cycles', :object=>Cycle.new))
Help please, how can I test this with cucumber?

Cucumber doesn't understand the "render" part of the request - what you'd want to do is find some content that exists in the "cycles" partial and check for that. For example, if your "cycles" partial contained the text "Here are the cycles for object <%= object.name %>", then your cucumber test would look like
find('div#cycle_form table').text.should have_content("Here are the cycles for object #{object.name}")
In that way, you can ensure that your cycles partial is rendering correctly.

Related

How to test that a view helper renders a partial?

Say I want to render different partials depending on an instance variable from the controller. I put the logic in a helper method, that looks something like:
def display_my_partial(foo)
foo == bar ? render(partial_x) : render(partial_y)
end
and in the view I call (using Slim):
= display_my_partial(#foo)
What should my test look like? I tried something like:
expect(display_my_partial(foo)).to render(partial: 'partial_x')
but got:
NoMethodError:
undefined method `matches?' for #<ActiveSupport::SafeBuffer:0x007ffb490aba80>
My example is a bit more complicated, as my partials are in a nested namespace. I had to experiment a little with just usind render 'partial_x' vs render partial: 'namespace/model/partial_x' to get it working in the specs, but finally I got the above mentioned error.
So how would you test this?
Where are you testing it in? Make sure render_views is called.
In any case, do you really care it's rendering that partial? What if the file name is changed, or you decide to change the implementation using html helpers instead. None of this matters to the output. I would personally assert the output instead. Depending on how complex the output is you could do it in a view test or just simple unit tests.
HTH,

How to test partials in views specs?

I want to have a user menu in my layouts/application.html.erb. In future, I plan to move it to a partial layouts/_user_menu.html.erb.
How should I start, write tests for this menu just in application.html.erb_spec.rb, and then as refactoring to move it to partial, leaving all tests in application.html.erb_spec.rb?
Or write its separate test _user_menu.html.erb_spec.rb? In this case, how can I test application.html.erb to render this partial? I don't thinks its good idea to use html selectors here, and think about something like in my application.html.erb_spec.rb:
expect(view).to render_partial 'user_menu'
Please try this
response.should render_template(:partial => 'partial_name')

How can a partial detect if it's being rendered by a mailer?

I have a partial that is being shared between a few different views, and a mailer template. This partial should attempt to use the user's session to store some state information if possible.
Determining if the session exists seems to be a bit of a problem. Within the partial, calling defined?(session) always seems to yield true during a mail render (is this a bug?), but attempting to access "session" in any way yields an "undefined method" exception.
As of now, I'm having my mailer use a #for_mailer instance variable to signal this partial to render differently, but this doesn't seem very elegant. Is there some simple way for the partial to figure out whether or not it's being rendered by a mailer, as opposed to being rendered in the context of a web request?
I would also create two partials for this but here is an alternative solution as well.
Assuming that it is coming from a different controller and action, you could check the params[:controller] and params[:action].
If you end up doing this more than a few times, you will probably end up with more code than just rewriting the partial. What do you want to be different between the two presentations?

Testing view helpers: how do I specify a controller?

Consider, for example, the following code:
class ViewHelpersTest < ActionView::TestCase
should 'generate tag with correct parameters' do
assert_equal theme_stylesheet_link_tag('style', :media => 'print'),
'<link href="/themes/default/stylesheets/style.css" media="print" rel="stylesheet" type="text/css" />'
end
end
current_theme_stylesheet_tag is a view helper that creates a stylesheet link tag that points to a css file located inside the current theme's directory. The current theme can be retrieved by calling ApplicationController::current_theme.
So, I need to provide a valid controller instance, which brings me to my question:
How exactly do I specify the controller to be used when testing view helpers in Rails 3?
Also, if there is a better way to express this test, please let me know. I have little experience with testing.
I'm not a big fan of testing helpers, but if you do it's best to approach it as a unit test, so you want to isolate the method from dependencies. In this case, instead of trying to create a controller object, you can just create a mock object and stub the necessary method call.
In RSpec this might work like so, assuming your current_theme method just returns a string:
describe ViewHelper do
it "should generate tag with correct parameters" do
ApplicationController = double(:current_theme=>"A string")
helper.my_helper_method("argument").should == "A string"
end
end
When the helper method executes ApplicationController::current_theme, it uses the stub instead of the actual method, so there's no need to instantiate a controller, or even to require the controller code.
I'm not the testing expert but I wonder if that should be a controller test instead of a view test. For example, Rails Guides describes view tests as "asserting the presence of key HTML elements and their content."
If you're setting the theme in the controller, can you test it there?

How to test link_to_unless_current in RSpec view example group

I'm learning RSpec 2 with Rails 3 and while it's been going along quite nicely so far, I'm having a problem testing the helper link_to_unless_current in a view. What I've been trying to do is use a simple assert_select from a view spec to determine if a link is being generated in the following partial view (HAML):
%article.post{ :id => "post-#{post.id}" }
%header.post-title
%h2= link_to_unless_current post.title, post
.post-content= raw post.body
However, I don't know how to get the view spec to recognize what "current" means because it's a view spec and it only tests the view, not the request. I know this would be a lot simpler in a controller spec, but I think that I should be testing what a view does in its spec and that moving this test out to a controller spec would be confusing things a lot. What I'm asking is: is there any way to tell the view spec, perhaps in a "before" block, what the current page is? Also, am I doing the right thing in respect to organizing my tests? Should this test rightfully reside in a controller spec?
Never mind, I eventually figured it out. You have to stub UrlHelper.current_page? and have it return true if the url options passed match the page you want to act as the current page:
view.stub("current_page?".to_sym) {|options| url_for(options) == post_path(#post) }
I still don't know if this is the way I should be doing RSpec tests, but, whatever, this works. :P

Resources