0

Or am I misunderstanding something?

Partial lives in:

app/views/admin/command_templates/_fillerup.html.erb

This spec passes (there is no "fill" or "_fill" in the view directory):

describe "GET fillerup" do
  it "assigns some stuff and renders a partial" do
    should render_template "admin/command_templates/_fill"     # I expected failure
    should render_template "admin/command_templates/_fillerup" # "Correct" test
  end
end

This fails (as I expect):

describe "GET fillerup" do
  it "assigns some stuff and renders a partial" do
    should render_template "admin/command_templates/_fillerupp" # extra "p"
  end
end

It acts like it's doing a start_with? on the path, perhaps to ignore extensions, or...?

rspec-rails 2.7.0, rails 3.1.2; other info available on request.

Verifying the behavior exists (or doesn't) would be helpful as a sanity check.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • This is somewhat orthogonal to your issue, but note that these tests are completely useless. You shouldn't normally care what partial is being rendered, or what's getting assigned; the only thing you should be testing once you get to the controller level is what the user sees -- so just test the rendered HTML (which is better done with Cucumber, not controller specs). – Marnen Laibow-Koser Dec 14 '11 at 18:08
  • @MarnenLaibow-Koser The snippets shown aren't indicative of the actual test; they're [SSCCEs](http://sscce.org/) to explain a behavior I was seeing. This part isn't in the tests anymore anyway. – Dave Newton Dec 14 '11 at 18:27
  • You call this retaliatory? [Allow me to point you here, and don't forget to view the vote split](http://stackoverflow.com/questions/1884804/checking-for-presence-of-net-remoting-server-is-my-approach-correct). – Michael Petrotta Jun 05 '12 at 05:12

1 Answers1

2

Check out the code for render_template - many of those methods do pattern matching via regex, which is going to match similarly to a starts_with?, at least in the same sense that the pattern "Joe" is going to match the string "Joe Smith", and "Joe Blank". I'm not doing the pattern matching justice here, but this is just an example.

Pattern matching this way is mostly for convenience, as I understand it, so you don't have to be overly specific in your tests, which makes your test framework a little less brittle. You can typically get away with this without worrying, because templates often don't have names that are so similar.

If you need to match only a specific template (and not the other) you can add an additional conditional test, something like:

should render_template('x')
should_not render_template('xy')

...or something similar, which should effectively filter out templates that start with similar names, but which are not the templates in which you are interested.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
  • Agreed, it's a (sort-of) edge case. Also makes the test useless, since a typo could go completely unnoticed :/ Thanks for looking in to it--I wanted another set of eyes on the RSpec code. – Dave Newton Dec 12 '11 at 14:43