Conversation
|
If you are going to use Regex, you might want to consider ignoring some whitespace and/or variable names if those aren't important. This test expect(template_contents).to match /<%= link_to "Show details", movie %>/will fail for this view code because it has a trailing space at the end <%= link_to "Show details", movie %>It would also fail if someone named their variable So it might be better to make the Regex more flexible, or be explicit about the requirements in the title and/or failure message. The simplest refactor might just be to test for This would change the Regex to something like expect(template_contents).to match /<%=.*link_to.*"Show details".*%>/ |
|
Perhaps we could use |
|
@raghubetina @jelaniwoods It might be time to re-open this question: Do we want to implement Phase 2 specs that read the codebase and ensure that helper methods are being utilized? Motivation: DPI will be going through these projects in the near future and they are interested in grades / progress. The current specs for e.g. the helper methods projects are very basic and do not have any checks that they are actually using the helper methods. |
jelaniwoods
left a comment
There was a problem hiding this comment.
Do we want to implement Phase 2 specs that read the codebase and ensure that helper methods are being utilized?
I think that's a good idea.
You can use view specs and stubbing to explicitly check for methods being called in views. It's much more robust than regex testing.
# spec/views/movies/show.html.erb_spec.rb
require "rails_helper"
describe "movies/show.html.erb" do
let!(:movie) { Movie.create(title: "E", description: "EE", created_at: 1.day.ago, updated_at: 0.day.ago) }
it "uses time_ago_in_words for created_at" do
assign(:the_movie, movie)
allow(view).to receive(:time_ago_in_words)
render
expect(view).to have_received(:time_ago_in_words).with(movie.created_at)
end
it "uses link_to for edit link" do
assign(:the_movie, movie)
allow(view).to receive(:link_to)
render
expect(view).to have_received(:link_to).with("Edit Movie", "/movies/#{movie.id}/edit")
end
end
So far, the previously added specs just test basic functionality.
A better way might be actually testing for the presence of the helper methods in the source code, rather than just testing the HTML output.
Things to potentially test for here:
link_toform_withinnew.html.erbandedit.html.erbfindin controllerquery_from form params and change from strings to symbolsresourcesinroutes.rbAn unconventional suggestion from @jelaniwoods that works well (and has already been added as a commit here):