2014-05-01 1 views
0

이것은 레일의 첫 프로젝트이며, 어떤 이유로 컨트롤러에 대한 첫 번째 단위 테스트를 만들지 못합니다.컨트롤러를 테스트 할 때 모델의 초기화되지 않은 상수

기본적으로, 나는 주된 선거를 갖고 있으며, 각 선거에는 많은 유권자가 포함될 수 있습니다. 유권자는 쉼표로 구분 된 전자 메일 목록으로 만들어집니다.

이 테스트에서는 여러 개의 전자 메일 목록을 테스트하여 올바르게 섭취했는지 확인하려고합니다. 하지만 실제로 파악할 수없는 이유로 인해 유권자 모델이 컨트롤러 테스트에서 발견되지 않습니다.

voters_controller_test.rb

require 'test_helper' 

class VotersControllerTest < ActionController::TestCase 

    test "should add new voters" do 
     assert_difference('Voters.count', 2) do 
      post :create, voter: {election_id: 1, email_list: "[email protected], [email protected]"} 
     end 
    end 
end 

voter.rb

class Voter < ActiveRecord::Base 
    attr_accessor :email_list 
    belongs_to :election 

    validates :email, presence: true, :email => true 
    validates_uniqueness_of :email, :scope => [:election_id] 
end 

컨트롤러, voters_controller.rb : 그래서 여기

코드의 관련 부분

class VotersController < ApplicationController 
    def index 
     @election = Election.find(params[:election_id]) 
    end 

    def create 
     @election = Election.find(params[:election_id]) 

     emails = voter_params[:email_list].squish.split(',') 
     emails.each { |email| @voter = @election.voters.create(:email =>email) } 

     redirect_to election_voters_path(@election) 

    end 

    private 

     def voter_params 
      params.require(:voter).permit(:email_list) 
     end 

end 

아마 내 응용 프로그램이 제대로 작동하며 테스트 만 실패하고 있다고 언급해야합니다.

정확한 오류 메시지는 다음과 같습니다

Run options: --seed 24993 

# Running: 

E. 

Finished in 0.098560s, 20.2922 runs/s, 10.1461 assertions/s. 

    1) Error: 
VotersControllerTest#test_should_add_new_voters: 
NameError: uninitialized constant VotersControllerTest::Voters 
    /home/jll/Documents/01_perso/00_myelections/test/controllers/voters_controller_test.rb:6:in `block in <class:VotersControllerTest>' 

이 내 첫 번째 루비 테스트, 나는 크게 the rails testing tutorial에서 자신을 영감이다.

내가 뭘 잘못했는지 약간의 통찰력을 제공해 주시겠습니까? 감사합니다.

+0

정확한 오류는 무엇입니까 ?? – Saurabh

+0

HA, 예, 가장 중요한 부분을 잊었습니다 ^^. 내 게시물 편집 – jlengrand

답변

3

Voter 모델 대신 Voters 모델에서 차이점을 주장하려고합니다. 이것은 코드가 어떻게 보일 것인가입니다.

컨트롤러에는 복수 이름이있는 반면 모델에는 리소스 이름의 단 하나의 버전이 있습니다. 예 : 모델은 Voter이고 컨트롤러는 VotersController입니다.

+0

하, 네 말이 맞아. Voter.count로 전환하면 오류가 사라집니다. 나는 단수형 또는 복수형을 사용할 때 여전히 혼란 스럽다. :) 감사! – jlengrand

+0

오신 것을 환영합니다. 다행이라고 생각합니다. 잠시 후, 그것은 모두 제 2의 천성이 될 것입니다! –