I'm trying to add my first test to my Rails project and I'm getting an error that I can't figure out.
Everything works fine (I can create articles) normally from the browser, but I'm getting this error in my test:
Test:
test "should create article" do
assert_difference('Article.count') do
post :create, article: {
title: 'Test Title',
text: 'This is my first article.',
status: 'published',
}, site_id: @site.id
end
assert assigns(:article).category_id == nil
assert_redirected_to site_articles_path(@site)
end
Running:
[project] rake test
E
Finished in 0.477441s, 2.0945 runs/s, 0.0000 assertions/s.
1) Error:
ArticlesControllerTest#test_should_create_article:
RuntimeError: can't modify frozen String
app/controllers/articles_controller.rb:30:in `create'
test/controllers/articles_controller_test.rb:12:in `block (2 levels) in <class:ArticlesControllerTest>'
test/controllers/articles_controller_test.rb:11:in `block in <class:ArticlesControllerTest>'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Here's what I think are the relevant parts of the controller:
class ArticlesController < ApplicationController
before_action :authenticate_user!
before_action :set_site
before_action :set_article, only: [:edit, :update, :destroy]
respond_to :html, :json
def create
@article = Article.new(article_params)
@article.site = @site
if @article.save
redirect_to site_articles_path(@site)
else
render 'new'
end
end
private
def set_site
@site = current_user.sites.find(params[:site_id])
add_crumb 'Articles', url_for(@site)
end
def set_article
@article = @site.articles.find(params[:id])
end
def article_params
params.require(:article).permit(:title, :text, :status, :category_id, :keywords, :row_order_position)
end
end
I believe these are the relevant parts of the Model:
class Article < ActiveRecord::Base
include AlgoliaSearch
belongs_to :site
belongs_to :category, counter_cache: true
validates :site, presence: true
validates :title, presence: true, length: { minimum: 5 }
enum status: [ :draft, :published ]
include RankedModel
ranks :row_order, :with_same => [:site_id, :category_id]
algoliasearch per_environment: true, force_utf8_encoding: true, sanitize: true do
attributes :title, :text, :keywords, :site_id
removeWordsIfNoResults 'allOptional'
# Listing attributes in order of priority.
# Use unordered so we don't boost based on if in beginning of attribute.
attributesToIndex ['unordered(title)', 'unordered(keywords)', 'unordered(text)']
attributesToHighlight ['title', 'text']
tags do
["site_#{site.id}", status]
end
end
end
Any ideas on why?
Or any suggestions on how to debug this?
I figured it out, turns out it was related to the Algolia tags
section in the model. I upgraded algolia-rails
package to a newer version and also changed:
["site_#{site.id}", status]
to:
["site_#{site.id}", status.dup]