我正试图将我的第一个测试添加到我的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
有什么想法吗?
或者对如何调试这个有什么建议?
最佳答案
我发现它和模型中的algoliatags
部分有关。我将algolia-rails
包升级到新版本,还更改了:
["site_#{site.id}", status]
到:
["site_#{site.id}", status.dup]
关于ruby-on-rails - Rails测试:RuntimeError:无法修改卡住的String,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34372983/