【问题标题】:RSPEC Redirect_To SyntaxRSPEC Redirect_To 语法
【发布时间】:2013-05-27 21:40:10
【问题描述】:

大家好—— 我是 rspec 测试的新手,并且正在关注 Aaron Sumner 的 dailyrailsrspec pdf。我在控制器测试中遇到了 redirect_to 的问题。我正在我的客户控制器中测试我的创建操作。创建记录后,我重定向到我的“列表”操作。尝试测试这让我很适应。

我的 rspec 控制器测试:

describe 'POST #create' do
  context "with valid attributes" do
    it "saves the new customer in the database" do
      expect{
        post :create, customer: attributes_for(:customer)
      }
    end

    it "redirects to list page" do
      post :create, customer: attributes_for(:customer)
      expect(response).to redirect_to(:action => list)
    end
  end
end

“它节省了...”测试通过了,但重定向没有通过。在 pdf 中,它显示了一个使用“customer_url”的示例。我从http://rspec.rubyforge.org/rspec-rails/1.1.12/classes/Spec/Rails/Matchers.html 获得了我使用的语法(上图),但它对我不起作用。

错误输出:

失败:

1) CustomersController while signed in POST #create with valid attributes redirects to list page
 Failure/Error: expect(response).to redirect_to(:action => list)
 NameError:
   undefined local variable or method `customer' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_5::Nested_1:0x007fdcbfe88f20>

我尝试将控制器名称添加到测试中,如 >> redirect_to(:controller => customers, :action => list),但它也失败了。

帮助?谢谢。

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    错误是说"customer is not defined"。我假设您使用的是 FactoryGirl,并且您定义了 customer 工厂。如果这是正确的,那么您在 customer 哈希中遗漏了 Factory 方法

    it "redirects to list page" do
      post :create, customer: Factory.attributes_for(:customer)
      expect(response).to redirect_to(:action => list)
    end
    

    你也可以这样写

    it "redirects to list page" do
      post :create, customer: Factory.attributes_for(:customer)
      response.should redirect_to(:action => list)
    end
    

    另外,在第一次测试中,我建议添加

    it "saves the new customer in the database" do
      expect{
        post :create, customer: Factory.attributes_for(:customer)
      }.to change(Customer,:count).by(1)
    end
    

    这应该让你走上正轨

    【讨论】:

    • 感谢您的帮助,但这并没有,抱歉。我确实有一个工厂供客户使用。我的 spec_helper.rb 中还有一行: # Include Factory Girl syntax to简化对工厂的调用 config.include FactoryGirl::Syntax::Methods 这使我不必在创建或构建语句中使用工厂。如果我删除第二个“it”块,所有测试都会按原样通过。但是这个不会。此外,添加“工厂”会给我一个错误:“未初始化的常量工厂”我的规范中有“需要 spec_helper”。
    • 如何添加换行符以便向这些 cmets 添加格式化代码?如果我按下 Return 键,它会提交我的评论。
    • 使用反引号`example而不是''example'
    • 我会拿出# Include Factory Girl syntax to simplify calls to factories config.include FactoryGirl::Syntax::Methods 并把它写出来,直到你真正理解rspec 和factorygirl 发生了什么。另外,这是您提到的同一作者的link to an article。这与我在回答中显示的基本相同。我最好的猜测是您的工厂设置方式正在发生一些事情。你能发布所有相关的工厂女孩​​文件吗?
    • 将阅读 Aaron Sumner 的文章。以下是我为 factoryGirl 填写的条目。希望够了。谢谢你的时间。我的客户工厂'代码'需要'faker' FactoryGirl.define do factory :customer do business_name "Parts R Us" contact_name "John" 地址 "1 Stewart Ave" city "Cortland" state "NY" zip "13045" customer_type "Contractor"电子邮件 { Faker::Internet.email } 电话 { Faker::PhoneNumber.phone_number } end end '/code'
    猜你喜欢
    • 2011-08-27
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多