2013-06-04 57 views
5

tôi đang tạo ra một loạt các thử nghiệm với hai nhà máy, xem dưới đây:RSpec lỗi NameError: liên tục chưa khởi tạo trên một máy

đặc tả/nhà máy/page.rb

FactoryGirl.define do 
    factory :page do 
     title "Example Title" 
     content "Here is some sample content" 
     published_on "2013-06-02 02:28:12" 
    end 
    factory :page_invalid do 
     title "" 
     content "" 
     published_on "2013-06-02 02:28:12" 
    end 
end 

Tuy nhiên, trong spec/điều khiển /page_controller_spec.rb, các thử nghiệm sau ném một lỗi:

describe "with invalid params" do 
    it "does not save the new page in the database" do 
    expect { 
     post :create, {page: attributes_for(:page_invalid)}, valid_session 
     }.to_not change(Page, :count).by(1) 
    end 

lỗi:

1) Api::PagesController POST create with invalid params does not save the new page in the database 
    Failure/Error: post :create, {page: attributes_for(:page_invalid)}, valid_session 
    NameError: 
     uninitialized constant PageInvalid 
    # ./spec/controllers/pages_controller_spec.rb:78:in `block (5 levels) in <top (required)>' 
    # ./spec/controllers/pages_controller_spec.rb:77:in `block (4 levels) in <top (required)>' 

Mã này tương tự như mã trong Rails hàng ngày Rspec vì vậy tôi không chắc chắn tại sao nhà máy page_invalid không được công nhận.

Trả lời

10

Hãy thử điều này:

FactoryGirl.define do 
    factory :page do 
    title "Example Title" 
    content "Here is some sample content" 
    published_on "2013-06-02 02:28:12" 
    end 
    factory :page_invalid, :class => "Page" do 
    title "" 
    content "" 
    published_on "2013-06-02 02:28:12" 
    end 
end 

Thông báo này: class => tùy chọn trên của bạn: nhà máy page_invalid.

+0

đã hoạt động, cảm ơn –

+0

Tôi gặp sự cố tương tự khi thử nghiệm động cơ có thể lắp. Điều này có ích. –

Các vấn đề liên quan