强制控制器使用 current_user 进行模拟

发布于 2024-09-08 07:03:49 字数 1931 浏览 5 评论 0原文

我试图在我的 RSpec 测试中指定我的控制器应该使用 current_user.projects.find() 而不是 Project.find() 我正在使用 Mocha 模拟框架并且正在尝试这样的事情:

controller.current_user.projects.expects(:find).returns(@project)

我已经模拟了 controller.stubs(:current_user).returns(@profile)

即使我使用 Project.find()< /代码>实施。如何测试我的控制器是否正在调用正确的对象?

编辑(添加附加代码):

我有项目和任务,项目有很多任务。 current_user 拥有的项目中的任务的 show 方法

这是用于显示控制器中

def show
    @project = current_user.projects.find_by_id(params[:cardset_id])

    if @project.nil?
      flash[:notice] = "That project doesn't exist. Try again."
      redirect_to(projects_path)
    else
      @task = @project.tasks.find_by_id(params[:id])
    end
  end

:这是不检查 cardsets 方法是否被调用的测试关闭 current_user 对象。

当前测试:

context "with get to show" do
  context "with valid project" do
    before(:each) do
      @project = Factory(:project)
      @task = Factory(:task)
      @profile = @project.profile
      ApplicationController.stubs(:require_user).returns(true)
      controller.stubs(:current_user).returns(@profile)

      Project.stubs(:find_by_id).returns(@project)
      @project.tasks.stubs(:find_by_id).returns(@task)
      get :show, :project_id => @project.id, :id => @task.id
    end

    it "should assign task" do
      assigns[:task].should_not be_nil
    end

    it "should assign project" do
      assigns[:project].should_not be_nil
    end
  end

  context "with invalid project" do
    before(:each) do
      Project.stubs(:find_by_id).returns(nil)
      get :show, :project_id => @project.id, :id => @task.id
    end

    it "should set flash" do
      flash[:notice].should match(/doesn't exist/i)
    end

    it "should redirect" do
      response.should redirect_to(cardsets_url)
    end
  end
end

I am trying to specify in my RSpec tests that my controller should use current_user.projects.find() instead of Project.find() I am using the Mocha mocking framework and was trying something like this:

controller.current_user.projects.expects(:find).returns(@project)

I have already mocked out controller.stubs(:current_user).returns(@profile)

This test passes with this even when I use the Project.find() implementation. How can I test that my controller is calling off of the correct object?

Edit (adding additional code):

I have Projects and Tasks, Project have many tasks. This is the show method for displaying a task in a project that is owned by current_user

Action in the controller:

def show
    @project = current_user.projects.find_by_id(params[:cardset_id])

    if @project.nil?
      flash[:notice] = "That project doesn't exist. Try again."
      redirect_to(projects_path)
    else
      @task = @project.tasks.find_by_id(params[:id])
    end
  end

This is the test that is not checking that the cardsets method was called off the current_user object.

Current Test:

context "with get to show" do
  context "with valid project" do
    before(:each) do
      @project = Factory(:project)
      @task = Factory(:task)
      @profile = @project.profile
      ApplicationController.stubs(:require_user).returns(true)
      controller.stubs(:current_user).returns(@profile)

      Project.stubs(:find_by_id).returns(@project)
      @project.tasks.stubs(:find_by_id).returns(@task)
      get :show, :project_id => @project.id, :id => @task.id
    end

    it "should assign task" do
      assigns[:task].should_not be_nil
    end

    it "should assign project" do
      assigns[:project].should_not be_nil
    end
  end

  context "with invalid project" do
    before(:each) do
      Project.stubs(:find_by_id).returns(nil)
      get :show, :project_id => @project.id, :id => @task.id
    end

    it "should set flash" do
      flash[:notice].should match(/doesn't exist/i)
    end

    it "should redirect" do
      response.should redirect_to(cardsets_url)
    end
  end
end

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

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

发布评论

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

评论(1

一身骄傲 2024-09-15 07:03:49

根据您告诉我们的一点信息,我认为您需要:

@profile.expects(:find).returns(@project)

Based on the little you've told us, I think you need:

@profile.expects(:find).returns(@project)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文