Testing rails partial views standalone - ruby-on-rails

I'm using Test/Unit with a standard rails 2.1 project. I would like to be able to test Partial Views in isolation from any particular controller / action.
It seemed as though ZenTest's Test::Rails::ViewTestCase would help, but I couldn't get it working, similarly with view_test http://www.continuousthinking.com/tags/view_test
Most of the stuff Google turns up seems quite out of date, so I'm guessing doesn't really work with Rails 2.1
Any help with this much appreciated.
Thanks,
Roland

We're using RSpec in our Rails 2.1 project, and we can do this sort of thing:
describe "/posts/_form" do
before do
render :partial => "posts/form"
end
it "says hello" do
response.should match(/hello/i)
end
it "renders a form" do
response.should have_tag("form")
end
end
However I don't know how much of that you can do with the vanilla Rails testing apparatus.

Found this which may be relevant:
http://broadcast.oreilly.com/2008/10/testing-rails-partials.html

Testing a view without the controller code is a dangerous thing. Your tests might pass but your application might throw an error. Always test against real life situations not artificial ones.

Related

Rails Tutorial: Reason for CGI.escapeHTML in Chapter 12 Exercise 2

For the Rails Tutorial, I know what the solution is. (It's also here.) I do not understand why escaping the HTML is necessary though. I guess I'm confused on what it does. Specifically, in the user_profile_test.rb file given by this tutorial, we have the following test:
test "profile display" do
get user_path(#user)
...
assert_select 'div.pagination'
#user.microposts.paginate(page: 1).each do |micropost|
assert_match micropost.content, response.body
end
end
Why is escaping the HTML not necessary in this test and is necessary in the chapter 12 exercise one?
You need to escape HTML, to sanitize your data before it hits your database.
From the Ruby Docs:
CGI::escapeHTML('Usage: foo "bar" <baz>')
# => "Usage: foo "bar" <baz>"
If you didn't use escapeHTML someone could put a <script> tag in there and potentially write some harmful code. Maybe it's a social network site and people are embedding some malware in their comment and then everyone that visits for page has a bad time as well as you.
As far as the test goes, I think that's the reason, because you can control the test. But at the same time I really don't see why wouldn't add escapeHTML to the test as well, but maybe it's just not required where you are studying.

How to write assertion that checks Controller method renders specific view?

I am running Rails 4 with Ruby 2.1.1.
I have in my Controller
def home
render :index
end
and I am writing a Unit::Test to check that the controller renders the view index with the method home.
How would I write the assertion? So far I have
test "home should have index" do
assert true
end
My test passes, of course, because I have assert true, but upon looking over the documentation, and having tinkered around, I am still unsure how to write a proper functional test for a Controller. Many sources are incomplete or vague, and seeing how no simple Stack Overflow question regarding writing a simple functional test using Unit::Test appears on Stack Overflow, I figured this would be a great question.
All I want is a clear answer to assert that my functional test checks that the home method renders index. Once I figure out how a basic functional test is written, I think I'll be able to proceed to testing the rest of my app.
Cheers
TL;DR I don't know how to write a basic functional test. How do I do that? Please be Unit::Test beginner friendly.
Use assert_template to assert that the request was rendered with the appropriate template file or partials.
test "home should have index" do
get :home
assert_template 'index'
end
Adding to infused's answer:
That is considered a functional test for a specific controller only, whose features and limitations are described in the RailsGuide here.
If your controller's name is StaticPagesController, the test should be in the file:
test/controllers/static_pages_controller_test.rb
The RailsGuide describes how to test for a rendered view here.
You can run the tests like this:
$ bundle exec rake test
or:
$ bundle exec rake test:functionals
seeing how no simple Stack Overflow question regarding writing a
simple functional test using Unit::Test appears on Stack Overflow
That's because Rails has its own testing framework. The file test/controllers/static_pages_controller_test.rb actually looks like this:
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test 'home should get index' do
get :home
assert_template :index
end
end
Note the superclass: ActionController::TestCase. As the RailsGuide notes, ActionController::TestCase includes MiniTest, which is mostly a drop in replacement for Test::Unit.

RailsTutorial Chapter 3 - I just cannot get my tests to pass

I'm working my way through the RailsTutorial book and have stalled in Chapter 3.
I'm beginning to feel like I'm going insane!
The autogenerated tests for the 'home' and 'contact' pages work fine, but try as I might I cannot get my test for the 'about' page to pass.
I have (and I tell no lie) created and the deleted the addional lines in the routes.rb file and the pages_controller.rb file and created the view file no fewer than 4 times just in case I had some strange non-printing characters. I must have checked my versions against the sample code from the book at least twice that many times!
I temporarily changed the 'about' test case into a second 'contact' test just to prove that the syntax of the test case is fine. It works OK.
To make matters worse, the 'about' page actually renders fine in the browser...
I've just created another new page called 'landing' and I get the same failure.
It's like the autogenerated tests were sprinkled with fairy dust!
Has anybody experienced similar?
I guessing that it's something glaringly obvious, but after 5 hours with no visible progress frustration has got the better of me...
I'm using Ubuntu 10.04, Ruby 1.9.2, Rails 3.0.3
RESOLVED An Issue with spork not reloading files
Sounds like you've been thorough. So this isn't working?
# pages_controller_spec.rb
describe "GET 'landing'" do
it "should be successful" do
get 'landing'
response.should be_success
end
end
# pages_controller.rb
def landing
end
# routes.rb
get "pages/landing"
# views/pages/landing.html.erb
blah

How do I write a spec to verify the rendering of partials?

I'm using rr (the mocking framework) and rspec with ruby-on-rails. Also, I'm using the collection short hand for partial rendering. My question: How do I correctly fill out the the following view spec?
describe 'my_view' do
before(:each) do
assigns[:models] = Array.new(10, stub(Model))
end
it "should render the 'listing' partial for each model" do
# help me write something that actually verifies this
end
end
I've tried a few examples from the rspec book, rspec docs, and rr docs. Everything I try seems to leave me with runtime errors in the test - not failed assertions. Rather than show all the transformations I've tried, I figured all I'd need if someone showed me one that actually worked. I'd be good to go from there.
I would suggest asserting the presence of some HTML that the "listing" partial should generate. Otherwise, it sounds like you're trying to assert that Rails is technically calling render on the partial? That's the job of the Rails core tests to prove such functionality.

Should I write rails tests with the def or test keyword?

This seems like a simple question but I can't find the answer anywhere. I've noticed that in general, tests in a Ruby on Rails app can be written as:
test "the truth" do
assert true
end
or
def the_truth
assert true
end
It seems newer material writes tests the first way, but I can't seem to find a reason for this. Is one favored over the other? Is one more correct? Thanks.
There has been a shift in recent years from short, abbreviated test names to longer, sentence-like test names. This is partly due to the popularity of RSpec and the concept that tests are specs and should be descriptive.
If you prefer descriptive test names, I highly recommend going with the test method. I find it to be more readable.
test "should not be able to login with invalid password" do
#...
end
def_should_not_be_able_to_login_with_invalid_password
#...
end
Also, because the description is a string it can contain any characters. With def you are limited in which characters you can use.
I believe the first method was implemented starting with Rails 2.2.
As far as I am aware, it simply improves readability of your code (as def can be any function while test is used only in test cases).
Good luck!
As Mike Trpcic suggests you should check out RSpec and Cucumber. I'd like to add that you should also take a look at:
Shoulda (http://github.com/thoughtbot/shoulda/tree/master)
Factory Girl (http://github.com/thoughtbot/factory_girl/tree/master)
Shoulda is a macro framework for writing concise unit tests for your models/controllers, while the second is a replacement for fixtures.
I would suggest doing your testing with either RSpec or Cucumber. I use both to test all my applications. RSpec is used to test the models and controllers, and Cucumber tests the Views (via the included Webrat functionality).

Resources