Wrong use of .t method in Rails template such
<%= (user.score).t(:context => 'foo') %>
Causes an error
undefined method `t' for 46:Fixnum
The method was called on number.
Now the question is how can this situation can be tested? Where the test code should live, so all the templates will be tested before rendering?
It depends on your choice of testing framework. With the Test::Unit stuff that comes with rails your controller and integration tests render views and so should catch this sort of error. Other forms of integration testing, such as cucumber should also pick up such things.
If you use rspec then you can write view specs: specs that test view rendering in isolation.
Lastly if you find yourself with lots of logic in your views that you want to test, you're probably better off extracting that logic into a helper and writing unit tests/specs for that helper.
AFAIK you don't test the templates itself. But you can test e.g. with cucumber if you get the view results you are expecting. This could be some sort of "template testing".
Related
I know integration tests are preferred but I need this to be ran in a controller test, I'm testing a gem injecting html code in the view, especially with xhr so this can't be run in a feature spec (if it can, please explain me how :) )
So with rspec controller tests you can assert a selector is present (with capybara) :
response.body.should have_selector('#foobar')
has_selector? will call the all method from capybara to find the selector.
What I want to do is get the last child of body and then assert that its id is something in particular.
AFAIK it's not possible to do this with have_selector.
What I would do is :
all('body:first-child').first.id.should == '#foobar'
However, with Capybara DSL, all is defined like this (more or less):
def all(*args)
page.all(*args)
end
And the page will be empty unless I use visit but it's for integrations specs.
How can I use capybara all method inside an rspec controller test ?
I can't test it right now but after some googling it seems like this would do the trick
def page
Capybara::Node::Simple.new(response.body)
end
Source
I have a markdown helper that looks like this in application_helper.rb.
How would you go about writing a test for something like this? Also, is this the best way to generate simple HTML from markdown?
Thanks!
def markdown(text)
Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true).render(text).html_safe
end
Two options come to mind:
Stub out Redcarpet::Markdown and create an expectation of what it should receive:
expect(Redcarpet::Markdown).to receive(:new).with(...) # `with` args omitted for brevity
markdown(text)
The problem here is that the coupling between the method and the test is very high. It's very difficult to refactor the method without breaking the test, even though the method actually functions the same.
Assert the correctness of the output text directly:
expect(markdown(text)).to eq 'This is markdownified'
This allows you to refactor better, but it seems more like an integration test, not a unit test, and is almost testing Redcarpet more than your own method. Unit tests should usually assume that the external methods they call work correctly.
This is one of those cases where testing feels redundant to me since the method is just a thin wrapper around another, and I would not be against not unit testing it, and rather ensuring that you have an integration test—if this is by having your spec be an integration test, then that's probably okay.
It's ultimately all up to you and your TDD workflow and overall testing principles though.
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.
How can I test a .js.rjs response in rails(2.3.8) functional test ?
You can take the simple path and verify the contents being returned seem correct with a functional test.
However, you'll probably get a lot more value from something like Capybara and Celerity that will let you do real integration testing with a live JavaScript engine and verify the RJS causes the page behave you expect.
http://github.com/jnicklas/capybara/blob/master/README.rdoc
There's assert_select_rjs in case you weren't aware of it (like I was). Its something like an assert_tag for RJS output.
In a Rails application I have a Test::Unit functional test that's failing, but the output on the console isn't telling me much.
How can I view the request, the response, the flash, the session, the variables set, and so on?
Is there something like...
rake test specific_test_file --verbose
You can add puts statements to your test case as suggested, or add calls to Rails.logger.debug() to your application code and watch your log/development.log to trace through what's happening.
In your test you have access to a bunch of resources you can user to debug your test.
p #request
p #response
p #controller
p flash
p cookie
p session
Also, remember that your action should be as simple as possibile and all the specific action execution should be tested by single Unit test.
Functional test should be reserved to the the overall action execution.
What does it mean in practice? If something doesn't work in your action, and your action calls 3 Model methods, you should be able to easily isolate the problem just looking at the unit tests. If one (or more) unit test fails, then you know which method is the guilty.
If all the unit tests pass, then the problem is the action itself but it should be quite easy to debug since you already tested the methods separately.
in the failing test use p #request etc. its ugly, but it can work
An answer to a separate question suggested
rake test TESTOPTS=-v
The slick way is to use pry and pry-nav gems. Be sure to include them in your test gem group. I use them in the development group as well. The great thing about pry and pry nav is you can step through your code with a console, so you can not only see the code as it's executed, but you can also enter console commands during the test.
You just enter binding.pry in the places in the code you want to trigger the console. Then using the 'step' command, you can move line by line through the code as it's executed.