登録ユーザーが投稿できる投稿コントローラーをテストしています。
it 'should allow registered user to create post' do
user = FactoryGirl.create(:user)
sign_in user
expect {
article_params = FactoryGirl.attributes_for(:post)
post :create, :article => article_params
}.to_not change(Post, :count)
response.should redirect_to(new_user_session_path)
flash[:alert].should == "You need to sign in or sign up before continuing."
end
そして、私はエラーが発生します
Failure/Error: post :create, :article => article_params
NoMethodError:
undefined method `mb_chars' for nil:NilClass
モデルにgenerate_slugがあるためです。Post.rb
include Translit
#slug
before_validation :generate_slug
def generate_slug
self.slug = translit(title)
end
そして、Translit.rbにはmb_charsがあります
# encoding: utf-8
module Translit
def translit (string)
table = {
"ё"=>"yo","№"=>"#",
"а"=>"a","б"=>"b","в"=>"v","г"=>"g",
"д"=>"d","е"=>"e","ж"=>"zh","з"=>"z",
"и"=>"i","й"=>"y","к"=>"k","л"=>"l",
"м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
"с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
"ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"'",
"ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya"
}
string = string.mb_chars.downcase
table.each do |translation|
string.gsub!(/#{translation[0]}/, translation[1])
end
string.parameterize
end
終わり
手伝ってくれてありがとう
私のfactories.rbは
FactoryGirl.define do
factory :user do
email "testspec@gmail.com"
password "password"
password_confirmation "password"
end
factory :post do
title "Deploying through ssh"
body "This is post about ssh"
end
end