Rails test: RuntimeError: can't modify frozen String
Rails test: RuntimeError: can't modify frozen String
我正在尝试将我的第一个测试添加到我的 Rails 项目中,但我遇到了一个我无法弄清楚的错误。
通常从浏览器一切正常(我可以创建文章),但我在测试中遇到此错误:
测试:
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
运行:
[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
以下是我认为控制器的相关部分:
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
我认为这些是模型的相关部分:
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
有什么想法吗?
或者关于如何调试的任何建议?
我弄明白了,原来它与模型中的 Algolia tags
部分有关。我将 algolia-rails
软件包升级到更新版本并更改了:
["site_#{site.id}", status]
至:
["site_#{site.id}", status.dup]
我正在尝试将我的第一个测试添加到我的 Rails 项目中,但我遇到了一个我无法弄清楚的错误。
通常从浏览器一切正常(我可以创建文章),但我在测试中遇到此错误:
测试:
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
运行:
[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
以下是我认为控制器的相关部分:
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
我认为这些是模型的相关部分:
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
有什么想法吗?
或者关于如何调试的任何建议?
我弄明白了,原来它与模型中的 Algolia tags
部分有关。我将 algolia-rails
软件包升级到更新版本并更改了:
["site_#{site.id}", status]
至:
["site_#{site.id}", status.dup]