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')
Related
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,
I'm trying to implement the following behavior:
You can see that i have pretty standart views structure for controller, but instead "actions" partial, i created folder which is named like an action and in them i have a partials for actions.
I Try use "prepend_view_path":
def set_view_paths
self.prepend_view_path ["#{Rails.root}/app/views/#{controller_name}/#{action_name}"]
end
But rails finds next:
Missing template "posts/index"
in ".../app/views/posts/index"
I need to add somthing like that in the end of the action:
render template: 'index'
Question:
Is there a more beautiful way to solve this problem?
Checkout this SO thread. From reading the thread it doesn't look like there is a clean way for you to solve this problem. The only way would be to override render.
I'm not sure why you are putting your template files into subfolders but, if it is not providing you with any benifit, it's probably easier to move index.html.erb, show.html.erb ect out of their subfolders and just put them under views/posts.
This will also make it easier for other developers (and yourself, later on) because you are conforming to Rails conventions.
In my controller I am using render update,but I want to update only if div element if present.
In order to check if div element is present, I am using page.select but its not responding with anything,neither error or output,here is my code
def post_update
render :update do |page|
page.replace_html('div_id':partial=>'my_partial') unless page.select("#div_id").blank?
end
end
is there anything I am missing here ?
You can't use ruby conditionals like that in rjs: that "unless" and page.select are executed on your server and have no idea what the contents of the DOM are.
I once wrote a blog post with a few ways of dealing with this. The short version is probably "generate Javascript fragments with your conditions". These days I'd stay away from rjs: render json or HTML and have some JavaScript on your page that knows what to do with it.
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.
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