У меня есть несколько моделей: Пользователь и Статья. У пользователя есть_много статей, и статья принадлежит пользователю.
Я пытаюсь написать тест для проверки метода Post
(ПРИМЕЧАНИЕ: я использую локали в своем приложении):
require 'spec_helper'
describe ArticlesController do
render_views
before(:each) do
@admin = FactoryGirl.create( :admin )
@user = FactoryGirl.create( :user )
end
describe "POST 'create'" do
before(:each){ @article = FactoryGirl.create(:article) }
describe "for signed-in admin" do
before(:each){ test_sign_in( @admin ) }
it "should create article" do
expect do
post :create, :locale => :en, :article => @article.attributes.merge( :content => "some" )
end.should change( Article, :count ).by(1)
end
end
end
end
Но я получаю такую ошибку:
1) ArticlesController POST 'create' for signed-in admin should create article
ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: id, user_id, created_at, updated_at
Failure/Error: post :create, :locale => :en, :article => @article.attributes.merge( :content => "some" )
# ./app/controllers/articles_controller.rb:15:in `create'
# ./spec/controllers/articles_controller_spec.rb:106:in `block (5 levels) in <top (required)>'
# ./spec/controllers/articles_controller_spec.rb:105:in `block (4 levels) in <top (required)>'
Как я могу это исправить?
Мои Фабрики:
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "email#{n}@factory.com" }
password 'qwerty'
factory :admin do
admin true
end
end
factory :article do
content 'text is here'
user
end
end
Мой контроллер:
class ArticlesController < ApplicationController
before_filter do
redirect_to :root unless current_user && current_user.admin?
end
def create
@article = Article.new( params[:article] )
if @article.save
redirect_to articles_path
flash[:success] = "It has been created!"
else
render 'new'
end
end
end
Модель статьи:
# == Schema Information
#
# Table name: articles
#
# id :integer not null, primary key
# user_id :integer
# content :text
# created_at :datetime not null
# updated_at :datetime not null
#
class Article < ActiveRecord::Base
belongs_to :user
attr_accessible :content
validates :content,
:presence => { :message => :presense_message },
:length => {
:maximum => 50000,
:message => :max_lenght_message
}
end
UPD: Приложение здесь: https://github.com/Loremaster/Chirch_app