我想测试这个destroy动作:
def destroy
@comment = Comment.find(params[:id])
@comment_id = @comment.id
if @comment.delete_permission(current_user.id)
@remove_comment = true
@comment.destroy
else
@remove_comment = false
head :forbidden
end
end
Run Code Online (Sandbox Code Playgroud)
我的规格如下:
describe "DELETE 'destroy'" do
describe 'via ajx' do
it "should be successful if permission true" do
comment = Comment.stub(:find).with(37).and_return @comment
comment.should_receive(:delete_permission).with(@user.id).and_return true
comment.should_receive(:destroy)
delete 'destroy', :id => 37
end
end
end
Run Code Online (Sandbox Code Playgroud)
我总是得到:
comment.should_receive....
expected: 1 time
received: 0 times
Run Code Online (Sandbox Code Playgroud)
原因:从不调用delete_permission?你对如何测试它有什么建议吗?
你告诉Comment.find要回来@comment,但你永远不会delete_permission对那个对象设定期望; 你将它设置为stub调用返回的值,即comment局部变量.
试试这个:
# As Jimmy Cuadra notes, we have no idea what you've assigned to @comment
# But if you're not doing anything super weird, this should work
@comment.should_receive(:delete_permission).with(@user.id).and_return(true)
@comment.should_receive(:destroy)
Comment.stub(:find).with(37).and_return(@comment)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7265 次 |
| 最近记录: |