Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何比较Rspec中的作业?_Ruby On Rails_Ruby_Rspec_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 如何比较Rspec中的作业?

Ruby on rails 如何比较Rspec中的作业?,ruby-on-rails,ruby,rspec,ruby-on-rails-3.2,Ruby On Rails,Ruby,Rspec,Ruby On Rails 3.2,我有以下几点 it 'should assign a new profile to user' do get :new assigns(:user_profile).should ==(Profile.new) end 但它不起作用。我分别试过“eql”和“equal”。为了知道@user\u profile的内容是否为profile.new,我如何比较它 我曾经做过一个变通方法,对指定的变量进行.class处理,检查它是否是概要文件,但我想停止这些不好的做法 谢谢。这里的问题是对象。

我有以下几点

it 'should assign a new profile to user' do
  get :new
  assigns(:user_profile).should ==(Profile.new)
end
但它不起作用。我分别试过“eql”和“equal”。为了知道@user\u profile的内容是否为profile.new,我如何比较它

我曾经做过一个变通方法,对指定的变量进行.class处理,检查它是否是概要文件,但我想停止这些不好的做法


谢谢。

这里的问题是
对象。通过设计两次调用new
会创建两个不同的对象,它们并不相等

1.9.2p318 :001 > Object.new == Object.new
 => false
你可以在这里做的一件事是

let(:profile){ Profile.new }

it 'should assign a new profile to user' do
  Profile.should_receive(:new).and_return profile
  get :new
  assigns(:user_profile).should eq profile
end

现在,在调用控制器操作时,您实际上并没有创建新的配置文件,但您仍然在测试
profile
是否正在接收
new
,您正在测试该方法的返回值是否由控制器分配给
@user\u profile

这里的问题是
对象。设计两次调用new
会创建两个不同的对象,它们不相等

1.9.2p318 :001 > Object.new == Object.new
 => false
你可以在这里做的一件事是

let(:profile){ Profile.new }

it 'should assign a new profile to user' do
  Profile.should_receive(:new).and_return profile
  get :new
  assigns(:user_profile).should eq profile
end

现在,当调用控制器操作时,您实际上并没有创建一个新的配置文件,但是您仍然在测试
配置文件
是否正在接收
new
,并且您正在测试该方法的返回值是否正由控制器分配给
@user\u profile

真棒兄弟,这正是我想要的!;-)事实上,没有特别的理由使
profile
的值成为实际的
profile
对象;它可能是任何东西。例如,
let(:profile){“一个新的概要文件”}
不会真正改变测试的功能。但这没关系,因为在控制器测试中,您不想测试
Profile::new
本身的功能。但是,如果在控制器操作中对
@user_profile
调用其他方法,这可能会导致消息预期错误。(另一个选项是
let(:profile){mock('profile')。as_null_object}
)棒极了,兄弟,这正是我想要的!;-)事实上,没有特别的理由使
profile
的值成为实际的
profile
对象;它可能是任何东西。例如,
let(:profile){“一个新的概要文件”}
不会真正改变测试的功能。但这没关系,因为在控制器测试中,您不想测试
Profile::new
本身的功能。但是,如果在控制器操作中对
@user_profile
调用其他方法,这可能会导致消息预期错误。(然后另一个选项是
let(:profile){mock('profile')。as_null_object}