2014-10-31 192 views
0

我正在做测试程序的想法介绍。我不知道如何测试他们想要的东西。Rails控制器测试

以下是我的测试。

require "rails_helper" 

describe PeopleController do 
    describe "#create" do 
    context "when person is valid" do 
     it "redirects to #show" do 

     post :create, FactoryGirl.build_stubbed(:person) 

     expect(response).to redirect_to(show_people_path) 
     end 
    end 

    context "when person is invalid" do 
     it "redirects to #new" do 
     pending "create this test" 
     end 
    end 
    end 
end 

我当然使用工厂的女孩。我尝试了几种方法。我真的不知道锄头来测试这个控制器。

任何见解都会很棒。

回答

0

我会使用FactoryGirl创建一个'无效'的人,并将其作为参数发送到post :create

要创建无效人员记录,为什么不在FactoryGirl中使用nested factories?根据在模型验证,你可以简单地这样做:

规格/工厂/ person.rb

FactoryGirl.define do 
    factory :person do 
    ... 
    factory :invalid_person do 
     ... 
     email nil 
     ... 
    end 
    end 
end 

在您的测试

context "when person is invalid" do 
    it "redirects to #new" do 
    post :create, FactoryGirl.build_stubbed(:invalid_person) 
    expect(response).to redirect_to action: :new 
    end 
end 
+0

感谢。这现在更有意义。然而,我的终端抛出一个未初始化的常量FactoryGirl错误... hrm .. – 2014-10-31 02:40:58