Skip to content

Instantly share code, notes, and snippets.

@blaisethomas
Created March 3, 2015 16:22
Show Gist options
  • Save blaisethomas/5ebcf2c0edb986b97c82 to your computer and use it in GitHub Desktop.
Save blaisethomas/5ebcf2c0edb986b97c82 to your computer and use it in GitHub Desktop.
Answers

###Q1 // Create a new Rails application called model_tdd. Add rspec_rails to the Gemfile. Install RSpec.

$ rails new model_tdd

# Gemfile
group :development, :test do
  gem "rspec-rails"
end

$ bundle exec rails generate rspec:install

###Q2 // Create a User model with an email attribute.

$ rails g model User email
$ rake db:migrate

###Q3 // Add a test to make sure that the email attribute is populated when a User record is created.

# spec/models/user_spec.rb
it "is invalid without an email" do
  expect(User.new(email: nil)).to have(1).errors_on(:email)
end

###Q4 // (Verify that the test is failing by running $ bundle exec rspec spec/models/user_spec.rb) Write some code to make the test pass.

# app/models/user.rb
validates :email, presence: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment