On Mar 18, 2008, at 4:26 PM, Nikos Dimitrakopoulos wrote:
> Hi there all. Sorry if the question sounds silly but i'm rather new at
> the 'mocking' stuff... So here is my problem (code helps more than
> talking):
>
> the model:
>
> class Item < ActiveRecord::Base
>  ...
>  has_many :images, :dependent => :destroy
>  ...
>
>  protected
>
>  def validate
>     errors.add(:images, "cannot be empty") if self.images.count
>  end
> end
>
>
> the spec:
>
> describe Item do
>  before(:each) do
>    @item = Item.new
>  end
>
>  ...
>
>  it "should have at least one image" do
>    @item.should have(1).error_on(:images)
>    @item.images.should_receive(:count).at_least(:once).and_return(1)
>    @item.should have(0).errors_on(:images)
>  end
> end
>

I would try:

it "should have an error with no images" do
        @item.should have(1).error_on(:images)
end

it "should be okay with one image" do
        @item.stub!(:images).and_return(mock("images",:count => 1))
        @item.should have(0).errors_on(:images)
end

JD
>
> Obviously i'm doing something wrong cause this doesn't work:
>
> 1)
> 'Item should have at least one image' FAILED
> expected 0 errors on :images, got 1
> ./spec/models/item_spec.rb:60:
> script/spec:4:
>
> Finished in 0.341594 seconds
>
> 10 examples, 1 failure, 5 pending
>
> (ignore the rest)
>
> Any help would be more than welcome :)
> -- 
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to