testing helpers with 'haml_tag' - ruby-on-rails

module FooHelper
def foo
haml_tag(:div) do
haml_content("bar")
end
end
end
When I test this I get:
NoMethodError: undefined method `haml_tag'
This code is perfectly valid and works in a development/production environment.
It's something to do with having the haml helpers properly loaded in the test environment.
Thanks!

It looks like the Rails test scaffold isn't including Haml::Helpers in its context. If you're using Test::Unit, you can probably just include it yourself in the test class. You'll also want to run Haml::Helpers#init_haml_helpers in the test setup so that all the Haml stuff is properly initialized.

http://haml-lang.com/docs/yardoc/Haml/Helpers.html#init_haml_helpers-instance_method

Related

Rails Unit Test A Class with .blank? method

I have a class, inside the class there is an instance method like the following:
def validates
#signature.blank?
end
I am doing unit testing on this class, when I try to run test, I get following error:
NoMethodError: undefined method `blank?' for #<String:0x5449061e>
However, when I stop using Rails blank?, and change it to the following it works:
if (!#signature || #signature.empty?)
Does unit testing in Rails allows use of Rails method?
You need to
require 'active_support/core_ext/object'
on top of your test file

How to test a Rails helper that depends on another Rails helper?

I am writing an RSpec spec to test a Rails helper. The problem is that the helper method that I'm testing depends on a method defined in a different helper. It may be a code smell to me, but I'm adding tests for legacy code and am not at a point where I can refactor. How can I test this Rails helper?
module FancyHelper
def fancy_method
html = "hello"
html << another_fancy_method
html
end
end
module OtherHelper
def another_fancy_method
"world"
end
end
require "spec_helper"
describe FancyHelper do
describe "#fancy_method" do
it "does what it's supposed to" do
helper.fancy_method.should match("hello")
# NoMethodError: undefined method `another_fancy_method'
end
end
end
This is what stubs are for. When testing your helper that depends on the other helper, you will want to stub the other helper to get a predictable value and complete the test.
EDIT: https://www.relishapp.com/rspec/rspec-mocks/docs/method-stubs thanks grantovich for the newer link.

Rspec test for a helper in my case

I am developing a Rails app. I have a helper under app/helpers/ , that's the
db_data_helper.rb.
The methods in this db_data_helper are mainly used to execute sql dump file to load data to database. And these methods in the helper are used in some Rake task.
Some sample code of the helper:
db_data_helper.rb:
module DbDataHelper
def self.load_data
# CODE TO EXECUTE SQL dump files
end
...
...
end
Now, I would like to test this helper in Rspec but I am not sure how to implement the test in Rspec for a helper like this. Could some one help me on this?
I'd simply create a class in my spec file:
class DummyDbDataHelper
extend DbDataHelper
end
describe DummyDbDataHelper do
it "tests the load_data method" do
DummyDbDataHelper.load_data.should ...
end
end

Adding Controller Macros in Rspec

Im trying to define some controller macros for Rspec. Im using rails 3 and have my macros defined in spec/support/macros/controller_macros.rb, that file looks like this:
module ControllerMacros
def self.login_admin
#code
end
end
in my spec helper I have:
config.include(ControllerMacros, :type => :controller)
So in my controller spec i just call login_admin in my admin tests but when ever i use the method i get
undefined local variable or method `login_admin' for #<Class:0xb6de4854> (NameError)
At first I assumed that controller_macros.rb wasn't being included but when I added a "puts" to the file but that showed the file was at least being executed.
I can't see anything wrong with my setup and copying the login_admin method into the describe block works fine so im not sure whats wrong with it.
Maybe I am late to that, but for new comers.
Here is a good examples of using macros:
http://osmose.6spot.com.br/2011/01/rails-resource-routing-spec-w-rspec/
when you include a module it's methods are visible inside examples.
But when you extend the module, it's methods are only visible outside examples.
It gives you ways to compose your macros for each situation.
Try
ControllerMacros.login_admin
or remove self from the method definition.
One line answer: Remove self from the method definition
Why? The methods of included modules are available in RSpec examples
The login_admin method defined in ControllerMacros will be available in your RSpec example as login_admin
To Be Specific:
Rewrite spec/support/macros/controller_macros.rb as
module ControllerMacros
def login_admin
#code
end
end
Then tell Rspec to include the Macros
config.include(ControllerMacros, :type => :controller)

How to test Rails :helper_methods that live in application.rb?

Here's my situation - I have a helper named LayoutHelper that I use to help me build my layout, and I'm trying to test the following method.
It has a method in LayoutHelper that looks similar to this:
def show_login_form?
return false if #hide_login_form || logged_in?
return true
end
I also have the following in my application.rb:
helper_method :logged_in?
def logged_in?
return #logged_in_user.present?
end
And finally, my test file looks like this:
require File.dirname(__FILE__) + '/../../test_helper'
class LayoutHelperTest < ActionView::TestCase
def test_hide_login_form
assert(show_login_form?)
hide_login_form
assert(!show_login_form?)
end
end
My problem now is that when I run this test, I get this:
NoMethodError: undefined method `logged_in?' for #<LayoutHelperTest:0xb7a7aca8>
app/helpers/layout_helper.rb:17:in `show_login_form?'
layout_helper_test.rb:12:in `test_hide_login_form'
I'm wondering if I'm doing this the correct Rails way, and if so, what I need to change in order to get this test to work.
I have to say, this stumped me at first - but I finally figured out what wasn't quite right.
Helpers are html generators, and should be built in a bubble - not knowing what's going on in sessions, controllers, etc. Your show_login_form? method isn't generating code, it's working with controller methods. It should be in application_controller.rb itself, and tested in controller tests.
Once you move it here, you should have no problem testing. Oh, and this might be a shorter, sweeter way to write the method itself:
def show_login_form?
!(#hide_login_form || logged_in?)
end
I hope this helps. It certainly got me to research helper tests, which I've neglected until now.

Resources