Created
September 10, 2012 23:27
-
-
Save elg0nz/3694775 to your computer and use it in GitHub Desktop.
Fixtures in Rails3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######## 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