How would you test this with remarkable?

0 views
Skip to first unread message

FoxDemon

unread,
Apr 29, 2010, 7:54:21 AM4/29/10
to Remarkable
class DiscussionsController < ApplicationController
def new
@discussion = Discussion.new
@discussion.comments.build
end
end


The Problem is '@discussion.comments.build'.
The first method call, @discussion.comments, returns an empty Array
(at least in the Rails console). But on what does the build method get
called? [].build results in an NoMethodError.


Btw, building a new Comment is necessary for the generation of nested
forms.

--
You received this message because you are subscribed to the Google Groups "Remarkable" group.
To post to this group, send email to remarka...@googlegroups.com.
To unsubscribe from this group, send email to remarkable-co...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/remarkable-core?hl=en.

Sébastien Nicouleaud

unread,
Apr 30, 2010, 7:43:03 AM4/30/10
to remarka...@googlegroups.com
Every time you call #comments_proc, it will generate a new list:
http://github.com/carlosbrando/remarkable/blob/master/remarkable_rails/lib/remarkable_rails/action_controller/macro_stubs.rb#L459
So all your stubs will be lost.


# Solution 1: Reuse discussion_proc

describe DiscussionsController do
mock_model :discussion

describe :get => :new do
expects :new, :on => Discussion, :returns => discussion_proc
expects :comments, :on => discussion_proc, :returns => discussion_proc
expects :build, :on => discussion_proc

should_assign :to => :discussion, :with => discussion_proc
end
end

# Solution 2: Make your own comments_proc

describe DiscussionsController do
mock_model :discussion

def self.comments_proc
proc { @comments ||= [mock_comment] }
end

describe :get => :new do
expects :new, :on => Discussion, :returns => discussion_proc
expects :comments, :on => discussion_proc, :returns => comments_proc
expects :build, :on => comments_proc

should_assign :to => :discussion, :with => discussion_proc
end
end
Reply all
Reply to author
Forward
0 new messages