添加gem 'rails-controller-testing'后Rails测试仍报assert_template错误
assert_template NoMethodError After Git Pull in Rails Hey, I’ve dealt with this exact problem on Rails teams before—let’s break down why this happens and walk through the fixes:
Core Background
First, a quick recap: assert_template was pulled out of Rails core starting in Rails 5, so you must have the rails-controller-testing gem installed to use it. Your local setup works because you added the gem, but your teammate’s machine is still missing it for one of these common reasons:
1. You Forgot to Commit the Gemfile/Gemfile.lock Changes
Double-check that you actually pushed the gem addition to Git:
- Run
git statuslocally to see ifGemfileandGemfile.lockare tracked and committed. - If they’re not, fix it with these commands:
Then have your teammate rungit add Gemfile Gemfile.lock git commit -m "Add rails-controller-testing gem to support assert_template" git pushgit pullagain to get the updated files.
2. Your Teammate Didn’t Run bundle install After Pulling
It’s easy to forget this step! Tell them to run this in the project root:
bundle install
If they use a local gem path (with bundle install --path vendor/bundle), they might need to clean old dependencies first:
bundle clean --force bundle install
3. There’s a Conflict in Gemfile.lock
If multiple people edited the Gemfile around the same time, Git might have created a conflict in Gemfile.lock. Have your teammate:
- Resolve the conflict in
Gemfile.lock(keep the version ofrails-controller-testingyou added) - Run
bundle installto regenerate a cleanGemfile.lock - Commit the resolved file and push if needed
4. The Gem Is in the Wrong Group in Gemfile
Make sure rails-controller-testing is in the :test group in your Gemfile—otherwise, it won’t load in the test environment. Your Gemfile should look like this:
group :test do gem 'rails-controller-testing' # Other test gems like rspec-rails, capybara, etc. end
If it’s outside the :test group, move it and re-run bundle install, then commit/push the updated files.
5. Their IDE/Test Runner Is Caching Dependencies
Sometimes IDEs (like VS Code or RubyMine) cache gem environments. Have them restart their IDE or reload the project’s gem setup to ensure the new gem is recognized.
Pro tip: Always commit both Gemfile and Gemfile.lock when adding/updating gems—this ensures everyone on the team gets the exact same dependency versions, avoiding "works on my machine" headaches.
内容的提问来源于stack exchange,提问作者Jacob Lockard

