I am writing tests for a Sinatra app that takes input from an API via a gem. Once I have the API response I need to test that the template has correctly rendered. The response of the API will be the HTML of the page that I am loading.
My first instinct was to write a test that looks like this:
describe 'the root path'
it 'should render the index view' do
get '/'
expect(last_response).to render_template(:index)
end
end
Unfortunately when I try this I get the following error: undefined method `render_template'
I was wondering if anyone has encountered this problem - it seems like it should be an easy fix, but I can't seem to find any documentation to help with it.
I'm currently not testing views at all because of time constraints, but I did have some limited successs with Rack::Test.
In theory you can say:
require 'rack/test'
include Rack::Test::Methods
def app
Sinatra::Application
end
describe 'it should render the index view' do
get '/'
expect(last_response).to be_ok
expect(last_response.body).to eq(a_bunch_of_html_somehow)
end
If I were to go this road again, since my views are haml, I could implement the a_bunch_of_html_somehow method using a call to Haml::Engine -- but I'm not sure whether that helps you.
I'm lifting this wholesale from the Sinatra site here -- the page is well worth a read.
We ended up scrapping this approach since it was better handled by integration testing tool suites such as Selenium or Capybara. There is no equivalent that I could find in the basic Sinatra Rspec suite that could do this - it made more sense to move it into a different scope
Related
Testing and Rspec are new to me. Currently I'm using Rspec with Shoulda and Capybara to test my application. It's all fine to test models, controllers, helpers, routing and requests. But what should I exactly test in views? Actually I want to test everything in views, including DOM, but I also don't want to overdone things.
These three things would be a good starting point
Use Capybara to go start at the root of your site, and have it click on links and whatever until it gets to the view you want tested.
Make sure what ever content is supposed to be on the page, is actually showing up on the page. So, if they 'user' went to the Product 1 page, make sure all the Product 1 content is actually there.
If different users see different content, make sure that's working. So, if Admin users see Admin-type buttons, make sure the buttons are they when the user is an Admin, and that aren't when the user isn't.
Those 3 things are a pretty good base. Even the first one is a big win. That will catch any kind of weird view syntax errors you may have accidentally introduced, as the syntax error will fail the test.
At my work we are using RSpec only to do unit testing.
For business testing or behavior testing we are using Cucumber that is much more readable for the business and IT guys.
It's like a contract that you sign with your business or it's like a documentation that you can execute.
Have a look at Cucumber: http://cukes.info/
I use view specs to verify that the view uses the IDs and classes I depend on in my jQuery code.
And to test different versions of the same page. E.g.:
I would not want to create two full request or feature specs to check that a new user sees welcome message A and a returning user welcome message B. Instead I would pick one of the cases, write a request or feature spec for it, and then a additional view spec that tests both cases.
Rails Test Prescriptions might be interesting for you, since it has a chapter dedicated to view testing.
I'm currently building a portfolio with a basic custom content management system and I'm having a problem with SQLite errors.
As the majority of the pages on the site are created using the CMS they are stored in a table.
I want to implement a way of catching errors in the url and rendering a nice looking error page - rather than the standard Rails "Action Controller Exception" page.
I've had a thorough Google and really struggled to find anything resembling my problem. I'm quite new to RoR, so it's not something that is immediately obvious to me - if there is such a simple solution.
If someone could at least point me in the right direction, it would be most appreciated.
Many thanks
The Exception page with the stacktrace is shown only in development mode.
In production, when there is a routing error i.e. some error in the url as you mention above, it will render the public/404.html page so all you need to do is customize that page to get a nice looking error page.
To test it in development, in the config/development.rb file, change
config.consider_all_requests_local = true
to config.consider_all_requests_local = false
Go gentle on me: I've got the flu and only a few of my neural synapses are firing!
Here's a simple requests test for RSpec:
require 'spec_helper'
describe 'Home Page' do
it 'should mention Home' do
get '/'
response.body.should have_content("Home")
end
end
Great. It works. Now I want to verify that there's an image loaded when visting the home page as well. I assume there's a matcher for images similar to have_content(), so I first go looking for the definition of have_content().
Not found in
https://www.relishapp.com/rspec/search?query=have_content
http://guides.rubyonrails.org/testing.html
http://api.rubyonrails.org/
But the I remember that RSpec has nifty naming rules for matchers, so (e.g.) even?() => be_even(). But even then, searching for "content" in the above doesn't find anything.
(As an aside, I'm pretty sure I'm not looking for the Capybara method of the same name, since I'm doing a get and not visit. Right?)
At the risk of getting this question rejected for being too vague: where the heck is this method coming from, and where do I learn what else I can pass to response.body.should?
RSpec request specs use Capybara, so you can use either get or visit.
If you want to just check that the page has an <img> element with the correct link, you could use:
response.should have_selector('img', :src => '...')
I've been looking for ways to test a Rails 3 app that has quite a lot of JS code for its rich interface. I tried with Capybara, but that didn't work out, so now I'm giving Jasmine a try. But I'm having a hard time understanding how I should go about it.
From what I gathered, Jasmine alone is good for testing the JS components of a site, but what if I want to test the interface directly? I need something like:
describe "Sign in"
Visit '/home'
When user clicks "Sign in" link
The sign in form should appear
Can I actually do something like that with Jasmine? So far, my tests are included on a results page generated by the rails-jasmine gem and obviously they run over that DOM, not over my site's DOM.
I'm now trying to use evergreen, but I get the same result (I can't even include jQuery).
Any ideas?
Thanks!
To answer your question - no, Jasmine will not inherently function in a way which will let you navigate your site and test at such a high level, like an integration test with Cucumber.
Jasmine is built primarily to test the API of your js, and so essentially you will only be able to test URL routes or something with it if they are part of your API.
For example, you can test how a Backbone Router will respond to your window's current location, or navigating from one location to the next, but that is because Backbone Routers explicitly handle URL locations.
This might help: http://railscasts.com/episodes/275-how-i-test
You could try using jasminerice with fixtures. If the intent is to test the JS DOM interaction, then fixtures would be the way to go.
But if the idea is to test the server interactions as well, then going with something like cucumber makes sense.
I'm trying to write some tests for emails generated with a mailer class using rspec and email_spec (Ruby on Rails 3.0.7)
I would like to check if the layout used for rendering the email is the layout that was specified in the mailer class.
Any idea on how to do this? I've spent 3 hours searching for a solution but couldn't find anything.
Thanks!
(I realize this is a pretty late response. You've probably found a solution already)
I won't be able to update this answer much, but does this page help? It describes how to check if a layout was rendered. You could make a get request with parameters (example here) then check if the result renders the layout you want it to render.
This is cheating a little bit, since you're not really checking which template got generated...
However, I just wanted to perform a quick sanity check that the right email is (probably) being generated - so this was good enough for my needs:
# In RSpec:
expect(ActionMailer::Base.deliveries.last.subject)
.to eq I18n.t("name.of.email.subject")
# In MiniTest:
assert_equal I18n.t("name.of.email.subject"),
ActionMailer::Base.deliveries.last.subject