Stuck on testing validation

Hello,

I must be doing something dumb, but here it goes…

why does this work?

before(:each) do
@work = Work.new
end

#for testing validates_presence_of :title
it “should require a title” do
@work.title = nil
@work.should_not be_valid
end

while this doesn’t:

def valid_work_attributes
{
:title => “guernica”,
:description => “lorem ipsum lorem ipsum”,
:date => Date.today,
:category => Category.new(:name => “belle epoque”)
}
end

before(:each) do
@work = Work.new
end

it “should require a title” do
@work.attributes = valid_work_attributes.except(:title)
@work.should_not be_valid
end

the latter example fails with “expected valid? to return false, got
true” for

@work.should_not be_valid

doesn’t make sense to me, and I’ve been stuck for a while already :stuck_out_tongue:

any help greatly appreciated, please bear with the newbie :slight_smile:
Oliver

Here is a very good example by Luke, which I think you should follow -

http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1

fyi,
Namrata

Hi Namrata,

thanks, that’s a great resource and I ended up beefing my tests based
on the recommendations there.

actually though, the problem I was having was that validations weren’t
being recognized by the test suite. later I found out that when
regenerating the scaffold for the app a while back, the database.yml
used the sqlite adapter instead of mysql (I’d forgotten about this) -
once I switched back to mysql, where the database was prepared, the
flunking tests starting passing. :stuck_out_tongue:

  • Oliver

2008/3/4, Namrata T. [email protected]: