I started my journey with TDD in Rails and ran into a small problem regarding tests to test the model, which I seem to be unable to find. Say I have a User model,
class User < ActiveRecord::Base validates :username, :presence => true end
and a simple test
it "should require a username" do User.new(:username => "").should_not be_valid end
This validates the presence check correctly, but what if I want to be more specific? For example, testing full_messages on an error object.
it "should require a username" do user = User.create(:username => "") user.errors[:username].should ~= /can't be blank/ end
My concern with the original attempt (using should_not be_valid) is that RSpec will not produce a descriptive error message. It simply says: โExpected valid?โ To return false, got the true value. โHowever, the second test case has a minor flaw: it uses the create method instead of the new method to access the error object.
I would like my tests to be more specific with respect to what they are testing, but at the same time there is no need to touch the database.
Does anyone have an entrance?
ruby-on-rails ruby-on-rails-3 rspec rspec-rails rspec2
Feech Sep 24 2018-11-11T00: 00Z
source share