2011-10-24 5 views
1

나는 잠시 동안 내 머리를 때리고 있었고 아무런 진전을 이루지 못했습니다. 나는이 작업을 테스트하기 위해 노력하고몽고이드와 RSpec에 대한 ID 문제

def create 
    @job = Job.new(params[:job]) 

    respond_to do |format| 
    if @job.save 
     flash[:notice] = "The Job is ready to be configured" 
     format.html { redirect_to setup_job_path(@job.id) } 
     format.json { head :ok } 
    else 
     format.html { redirect_to new_job_path, notice: 'There was an error creating the job.' } 
     format.json { render json: @job.errors, status: :unprocessable_entity } 
    end 
    end 
end 

:

나는 다음과 같은 컨트롤러 액션이있다. 여기 성공적인 제작에 대한 리디렉션 테스트가 있습니다.

let (:job) { mock_model(Job).as_null_object } 

나는 다음과 같은 오류가 계속 :

it "redirects to the Job setup" do 
    job.stub(:id=).with(BSON::ObjectId.new).and_return(job) 
    job.stub(:save) 
    post :create 
    response.should redirect_to(setup_job_path(job.id)) 
end 

작업이 여기에 전체 제품군에 대한 정의되지 않은 상관없이 내가 몇 가지 다른 일을 시도했습니다

2) JobsController POST create when the job saves successfully redirects to the Job setup 
Failure/Error: response.should redirect_to(setup_job_path(job.id)) 
    Expected response to be a redirect to <http://test.host/jobs/1005/setup> but was a redirect to <http://test.host/jobs/4ea58505d7beba436f000006/setup> 

을하지만 I 시험에서 적절한 객체 ID를 얻지 못하는 것입니다.

답변

1

:id=을 스텁으로 작성하면 매우 약한 테스트가 생성됩니다. 실제로 몽고이 내부에 대한 자신감이 없다면 몽고이드가 이드를 생성하는 방식을 변경하면 테스트가 중단 될 가능성이 있습니다. 사실, 작동하지 않습니다.

또한 job 변수를 만들지 만이 변수는 컨트롤러 내부에 전달하지 않습니다. 그것은 :create 조치는

@job = Job.new(params[:job]) 

에서 자신의 작업 인스턴스를 초기화하고는 완전히 job을 무시 의미한다.

assigns을 사용하시기 바랍니다.

it "redirects to the Job setup" do 
    post :create 
    response.should redirect_to(setup_job_path(assigns(:job))) 
end 
+0

고맙습니다! 저는 rspec에 익숙하지 않고 배정 된 것을 잊어 버리는 것 같습니다. – LeakyBucket