Skip to content

Instantly share code, notes, and snippets.

@elg0nz
Created September 10, 2012 23:27
Show Gist options
  • Save elg0nz/3694775 to your computer and use it in GitHub Desktop.
Save elg0nz/3694775 to your computer and use it in GitHub Desktop.
Fixtures in Rails3
######## intros.yml
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
activity: MyString
why: MyText question: MyString
friend_a: MyString
friend_b: MyString
two:
activity: MyString
why: MyText
question: MyString
friend_a: MyString
friend_b: MyString
######## test/unit/intro_test.rb
require 'test_helper'
class IntroTest < ActiveSupport::TestCase
test "do not save empty intros" do
intro = Intro.new assert !intro.save
end
test "can create a valid intro" do
intro = Intro.new
intro.activity = "Get a coffee sometime"
intro.why = "Because you love both love fancy coffee"
intro.friend_a = "John"
intro.friend_b = "Jane"
intro.question = "What kind of beans do you like?"
assert intro.save
end
test "test intro validators." do
self.assert_cant_be_empty(Intro, [:activity, :question, :why])
end
def assert_cant_be_empty(model_class, attributes)
attributes.each do |attribute|
record = model_class.new(attribute => '')
assert !record.valid?
assert !record.errors.empty?
end
end
end
######## test/functional/intros_controller_test.rb
require 'test_helper'
class IntrosControllerTest < ActionController::TestCase
test "get index" do
get :index assert_response :success
assert_not_nil assigns(:intros)
end
test "create intro" do
assert_difference('Intro.count') do
post :create, :intro => {
:activity => "Get a coffee sometime",
:why => "Because you love both love fancy coffee",
:friend_a => "John",
:friend_b => "Jane",
:question => "What kind of beans do you like?"
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment