rspec 存根 current_company

发布于 2024-10-18 06:44:06 字数 762 浏览 4 评论 0原文

我有一个 Rails 3 项目,我想在其中存储在会话变量中选择的当前公司。

我正在使用员工控制器规范,并且想暂时删除 current_company,因为我正在隔离员工新控制器操作的规范示例。

it "should call current_company" do 
  company = mock_model(Company, :id => "1")
  controller.should_receive(:current_company).and_return(company)
  get :new
end

这是我对员工控制器的新操作,

  def new
    @staff = Staff.new
    @staff.company_id = current_company.id
  end

我不断收到错误,

Failure/Error: get :new
     NameError:
       undefined local variable or method `current_company' for #<StaffsController:0x000000028d6ad8>

我也尝试将其删除,而不是使用 should_receive

  controller.stub!(:current_company).and_return(company)

我收到相同的错误。

I have a Rails 3 project in which I want to store the current company selected in a session variable.

I'm working with the staff controller spec and would like to stub out current_company for now as I'm isolating my spec example for the staff new controller action.

it "should call current_company" do 
  company = mock_model(Company, :id => "1")
  controller.should_receive(:current_company).and_return(company)
  get :new
end

Here is my new action for the staff controller

  def new
    @staff = Staff.new
    @staff.company_id = current_company.id
  end

I keep getting error

Failure/Error: get :new
     NameError:
       undefined local variable or method `current_company' for #<StaffsController:0x000000028d6ad8>

I've also tried just stubbing it out instead of using should_receive

  controller.stub!(:current_company).and_return(company)

I get the same error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2024-10-25 06:44:06

你的代码对我来说看起来很好,它应该可以工作。一定还有其他我们没有看到的问题。我注意到控制器名称是“StaffsController”——正确吗?仔细检查控制器的名称和相应的规格——它们应该是相同的。

Your code looks fine to me, it should work. There must be some other problem we are not seeing. I notice the controller name is "StaffsController" -- is that correct? Double-check the names of the controller and the corresponding spec -- they should be the same.

终难愈 2024-10-25 06:44:06

我认为它在“应该成功”的示例/测试中被轰炸了,所以我把我的存根放在了之前的块中。

require 'spec_helper'

describe StaffsController do

  describe "GET 'new'" do
    let(:staff) { mock_model(Staff, :company_id= => nil)}
    let(:company) { mock_model(Company, :id => 1)}

    before do
      Staff.stub!(:new).and_return(staff)
      controller.stub!(:current_company).and_return(company)
    end

    it "should be successful" do
      get :new
      response.should be_success
    end

    it "should call current_company" do 
      controller.should_receive(:current_company).and_return(company)
      get :new
    end
  end
end

这适用于:

class StaffsController < ApplicationController
  def new
    @staff = Staff.new
    current_company.id
  end
end

I think it was bombing out on the 'should be successful' example/test, so I've put my stubbing in a before block.

require 'spec_helper'

describe StaffsController do

  describe "GET 'new'" do
    let(:staff) { mock_model(Staff, :company_id= => nil)}
    let(:company) { mock_model(Company, :id => 1)}

    before do
      Staff.stub!(:new).and_return(staff)
      controller.stub!(:current_company).and_return(company)
    end

    it "should be successful" do
      get :new
      response.should be_success
    end

    it "should call current_company" do 
      controller.should_receive(:current_company).and_return(company)
      get :new
    end
  end
end

This works for:

class StaffsController < ApplicationController
  def new
    @staff = Staff.new
    current_company.id
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文