0

我的控制器新操作有以下 rspec2 测试用例

describe "GET #new" do
  it "should assign new project to @project" do
    project = Project.new
    get :new
    assigns(:project).should eq(project)
  end
end

我收到以下错误

  1) ProjectsController GET #new should assign new project to @project
     Failure/Error: assigns(:project).should eq(project)

       expected: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil>
            got: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil>

       (compared using ==)

       Diff:#<Project:0x007f461c498270>.==(#<Project:0x007f461c801c90>) returned false even though the diff between #<Project:0x007f461c498270> and #<Project:0x007f461c801c90> is empty. Check the implementation of #<Project:0x007f461c498270>.==.
     # ./spec/controllers/projects_controller_spec.rb:19:in `block (3 levels) in <top (required)>'

Finished in 1.21 seconds
13 examples, 1 failure, 10 pending

Failed examples:

rspec ./spec/controllers/projects_controller_spec.rb:16 # ProjectsController GET #new should assign new project to @project

当我==改用 on 时eq,我收到以下错误

  1) ProjectsController GET #new should assign new project to @project
     Failure/Error: assigns(:project).should == project
       expected: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil>
            got: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil> (using ==)
       Diff:#<Project:0x007f461c4f5420>.==(#<Project:0x007f461c63b280>) returned false even though the diff between #<Project:0x007f461c4f5420> and #<Project:0x007f461c63b280> is empty. Check the implementation of #<Project:0x007f461c4f5420>.==.

我在这里做错了什么,在此先感谢

我上线了

  • Rails3
  • 规格2
4

1 回答 1

1

在访问新操作之前,您正在创建一个新项目,这是不必要的。您的控制器实际上已经为您工作了。您面临的问题是您创建了两个新项目(在您的情况下,您首先创建了 Project:0x007f461c498270,然后创建了 Project:0x007f461c801c90,它们具有相同的属性但是不同的项目)。此测试应通过:

describe "GET #new" do
  it "assigns a new Project to @project" do
    get :new
    assigns(:project).should be_a_new(Project)
  end
end
于 2013-02-06T12:15:06.713 回答