나는 attr_accessible이 선언 된 모델을 사용하는 컨트롤러를 테스트하기 위해 rspec을 사용하고 있습니다. 나는 attr_accesible이 작동하는지 테스트하고 싶지는 않지만 (나의 모델 스펙은 그렇게한다), 내 컨트롤러가 대량 할당을하고 있지 않은지 확인하고 싶다.질량 지정이없는 rspec 컨트롤러 사양 model_mock 작성 방법
class Post < ActiveRecord::Base
attr_accessible :body
validates :body, :presence => true,
validates :user, :presence => true
belongs_to :user
end
과 약간 발생 불통 제어기 (제거 XML 형식 선) :
구체적으로, I는 모델과 같이이
def create
# this line keeps the rspec test mock happy
@post = Post.new(params[:post].merge(:user => current_user))
# Below are the correct lines of code for runtime for attr_accessible
# @post = Post.new(params[:post])
# @post.user = current_user
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
I는 모의 제어기와 RSpec에 시험을 갖도록 위와 합격 :
describe "POST create" do
describe "with valid params" do
it "assigns a newly created post as @post" do
Post.stub(:new).
with({'body' => 'hi there', 'user' => @user}) { mock_post(:save => true) }
post :create, :post => {'body' => 'hi there'}
assigns(:post).should be(mock_post)
end
end
end
내가 원하는 것은 rspec 테스트를 첫 번째 @post 줄을 주석 처리하고 다음 두 개의 @post 줄의 주석을 제거 할 때 컨트롤러의 유효성을 올바르게 검사하십시오. 그렇게하면 컨트롤러가 실제 모델과 올바르게 작동하는지 확인할 수 있지만 테스트를 위해 mock을 계속 사용할 수 있습니다. 몇 가지 접근법을 시도하고 서클에서 돌아 다니는 것처럼 보입니다. (예, 레일스와 Rspect의 초보자입니다.)
미리 감사드립니다.
물론 (해당 ID가이 예에서이없는 가정) 컨트롤러에 의해 할당 된 USER_ID이 있는지 확인
그 요청에 나쁜 사용자 ID를 보내, 그 것 rspec 행복하지만, 내 컨트롤러 코드가 대량 할당을 사용하고 있지 않은지 테스트 할 것인가? – mm2001
아, 질량 지정 참조를 놓쳤습니다. 변경된 답변 – giladbu