2015-07-28 61 views
0

你好,我是在做我的Rspec的应用程序的一些测试(这是我的第一次,我用它)红宝石未初始化不断的RecipesController ::食谱

,这是我的测试文件位于规格/控制器/ recipes_controller_spec.rb

require 'spec_helper' 

describe RecipesController do 
    render_views 
    describe "index" do 
    before do 
     Recipe.create!(name: 'Baked Potato w/ Cheese') 
     Recipe.create!(name: 'Garlic Mashed Potatoes') 
     Recipe.create!(name: 'Potatoes Au Gratin') 
     Recipe.create!(name: 'Baked Brussel Sprouts') 

     xhr :get, :index, format: :json, keywords: keywords 
    end 

    subject(:results) { JSON.parse(response.body) } 

    def extract_name 
     ->(object) { object["name"] } 
    end 

    context "when the search finds results" do 
     let(:keywords) { 'baked' } 
     it 'should 200' do 
     expect(response.status).to eq(200) 
     end 
     it 'should return two results' do 
     expect(results.size).to eq(2) 
     end 
     it "should include 'Baked Potato w/ Cheese'" do 
     expect(results.map(&extract_name)).to include('Baked Potato w/ Cheese') 
     end 
     it "should include 'Baked Brussel Sprouts'" do 
     expect(results.map(&extract_name)).to include('Baked Brussel Sprouts') 
     end 
    end 

    context "when the search doesn't find results" do 
     let(:keywords) { 'foo' } 
     it 'should return no results' do 
     expect(results.size).to eq(0) 
     end 
    end 

    end 
end 

当我尝试通过命令来执行它:

bundle exec rspec spec/controllers/recipes_controller_spec.rb

我失败了我所有的工商业污水附加费与此错误TS:

Failure/Error: xhr :get, :index, format: :json, keywords: keywords 
    NameError: 
     uninitialized constant RecipesController::Recipes 
    # ./app/controllers/recipes_controller.rb:4:in `index' 
    # ./spec/controllers/recipes_controller_spec.rb:12:in `block (3 levels) in <top (required)>' 

我试着看我所有的代码,但我还没有找出错误

+0

它位于您的控制器第4行。您是否也可以通过此文件? – BroiSatse

+0

看起来像'recipes_controller'的'index'方法中有'Recipes'而不是'Recipe'。 – Pavan

回答

1
NameError: uninitialized constant RecipesController::Recipes 

意味着你index使用Recipes代替Recipe某处(4号线)在控制器中,并且由于您的模型被称为Recipe(单数),您会遇到NameError异常。