强制控制器使用 current_user 进行模拟
我试图在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您告诉我们的一点信息,我认为您需要:
Based on the little you've told us, I think you need: