Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

test error:- ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Everytime i do "ruby -Itest test/controllers/statuses_controller_test.rb" but i get 10 error with the same error.i don't know where i went wrong.

$ ruby -Itest test/controllers/statuses_controller_test.rb

DL is deprecated, please use Fiddle

Run options: --seed 51484

Running:

EEEEEEEEEE

Finished in 0.096356s, 103.7818 runs/s, 0.0000 assertions/s.

1) Error: StatusesControllerTest#test_should_get_edit: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_get_edit: NoMethodError: undefined method `each' for nil:NilClass

2) Error: StatusesControllerTest#test_should_render_the_new_page_when_logged_in: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_render_the_new_page_when_logged_in: NoMethodError: undefined method `each' for nil:NilClass

3) Error: StatusesControllerTest#test_should_create_status_for_the_current_user_when_logged_in: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_create_status_for_the_current_user_when_logged_in: NoMethodError: undefined method `each' for nil:NilClass

4) Error: StatusesControllerTest#test_should_update_status: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_update_status: NoMethodError: undefined method `each' for nil:NilClass

5) Error: StatusesControllerTest#test_should_destroy_status: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_destroy_status: NoMethodError: undefined method `each' for nil:NilClass

6) Error: StatusesControllerTest#test_should_get_index: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_get_index: NoMethodError: undefined method `each' for nil:NilClass

7) Error: StatusesControllerTest#test_should_be_logged_in_to_post_a_status: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_be_logged_in_to_post_a_status: NoMethodError: undefined method `each' for nil:NilClass

8) Error: StatusesControllerTest#test_should_be_redirected_when_not_logged_in: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_be_redirected_when_not_logged_in: NoMethodError: undefined method `each' for nil:NilClass

9) Error: StatusesControllerTest#test_should_show_status: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_show_status: NoMethodError: undefined method `each' for nil:NilClass

10) Error: StatusesControllerTest#test_should_create_status_when_logged_in: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: StatusesControllerTest#test_should_create_status_when_logged_in: NoMethodError: undefined method `each' for nil:NilClass

10 runs, 0 assertions, 0 failures, 10 errors, 0 skips

3 Answers

My statuses_controller.rb file look like this.

class StatusesController < ApplicationController

before_action :set_status, only: [:show, :edit, :update, :destroy]

before_filter :authenticate_user!, only: [:new, :create, :edit, :update]

# GET /statuses

# GET /statuses.json

def index

@statuses = Status.all

end

# GET /statuses/1 # GET /statuses/1.json def show end

# GET /statuses/new

def new

@status = Status.new

end

# GET /statuses/1/edit

def edit

end

POST /statuses

# POST /statuses.json

def create

@status = current_user.statuses.new(parms[:status])


respond_to do |format|

  if @status.save

    format.html { redirect_to @status, notice: 'Status was successfully created.' }

    format.json { render :show, status: :created, location: @status }

  else

    format.html { render :new }

    format.json { render json: @status.errors, status: :unprocessable_entity }

  end

end

end

# PATCH/PUT /statuses/1

# PATCH/PUT /statuses/1.json

def update

respond_to do |format|

  if @status.update(status_params)

    format.html { redirect_to @status, notice: 'Status was successfully updated.' }

    format.json { render :show, status: :ok, location: @status }

  else


    format.html { render :edit }

    format.json { render json: @status.errors, status: :unprocessable_entity }

  end

end

end

# DELETE /statuses/1

# DELETE /statuses/1.json

def destroy

@status.destroy

respond_to do |format|

  format.html { redirect_to statuses_url, notice: 'Status was successfully destroyed.' }



  format.json { head :no_content }

end

end

private

# Use callbacks to share common setup or constraints between actions.

def set_status

@status = Status.find(params[:id])

  end


# Never trust parameters from the scary internet, only allow the white list through.

def status_params

  params.require(:status).permit(:name, :context, :user_id)

end

end

My statuses_controller_test.rb file look like this.

require 'test_helper'

class StatusesControllerTest < ActionController::TestCase

setup do

@status = statuses(:one)

end

test "should get index" do

get :index

assert_response :success

assert_not_nil assigns(:statuses)

end

test "should be redirected when not logged in" do

get :new

assert_response :redirect

assert_redirected_to login_path

end

test "should render the new page when logged in" do

sign_in user(:sarah)

get :new

assert_response :success

 end

test "should be logged in to post a status" do

post :create, status: { context: "Hello" }

 assert_response :redirect

assert_redirected_to new_user_session_path

end

test "should create status when logged in" do

sign_in user(:sarah)

assert_difference('Status.count') do

  post :create, status: { context: @status.context }

end

assert_redirected_to status_path(assigns(:status))

end

test "should create status for the current user when logged in" do

sign_in user(:sarah)

assert_difference('Status.count') do

  post :create, status: { context: @status.context, user_id: users(:ruby).id }

end

assert_redirected_to status_path(assigns(:status))

assert_equal assigns(:status).user_id, user(:sarah).id

end

test "should show status" do

get :show, id: @status

assert_response :success

end

test "should get edit" do

get :edit, id: @status

assert_response :success

end

test "should update status" do

patch :update, id: @status, status: { context: @status.context}

end

test "should destroy status" do

assert_difference('Status.count', -1) do

  delete :destroy, id: @status

end

assert_redirected_to statuses_path

end

end

Nathan F.
Nathan F.
30,773 Points

This looks okay, but this seems to be your StatusesController. What we want is your statuses_controller_test.rb file, under tests -> functional, which should look a little like this (yours may differ)

Edit: Ooops! I see you just added the test file. Let me take a look.

when i run rake test comment i get this error

$ rake test

DL is deprecated, please use Fiddle

rake aborted!

test_a_user_should_have_a_unique_profile_name is already defined in UserTest C:/Users/Arvind/project/book/test/models/user_test.rb:30:in <class:UserTest>' C:/Users/Arvind/project/book/test/models/user_test.rb:3:in<top (required)>' Tasks: TOP => test:run (See full trace by running task with --trace)

And My user_test.rb file look like this...

require 'test_helper'

class UserTest < ActiveSupport::TestCase

test "a user should enter a first name" do user = User.new assert !user.save assert !user.error[:first_name].empty? end

test "user should enter a last name" do user = User.new assert !user.save assert !user.errors[:last_name].empty? end

    test "user should enter a profile name" do
            user = User.new
            assert !user.save
            assert !user.errors[:profile_name].empty?
    end

    test "a user should have a unique profile name" do
            user = User.new
            user.profile_name = users(:test).profile_name
            assert !user.save
            assert !user.errors[:profile_name].empty?
    end

   test "a user should have a unique profile name" do
            user = User.new
            user.profile_name = user(:sarah).profile_name
            assert !user.save   
            assert !user.errors[:profile_name].empty?
    end

        test "a user should have profile name without spaces" do
            user = User.new(first_name: "sarah", last_name: "gupta", email: "sarahgupta022@gmail.com")
            user.password = user.password_confirmation = "welcome"
            user.profile_name = "My Profile With  Space"
            assert !user.save
            assert !user.errors[:profile_name].empty?
            assert user.errors[:profile_name].include?("Must be formatted correctly.")
    end

    test "a user can have a correctly formatted profile name" do
      user = User.new(first_name: "sarah", last_name: "gupta", email: "sarahgupta022@gmail.com")
      user.password = user.password_confirmation = "welcome"
      user.profile_name = "Sarah"
      assert.user.valid?
    end

end

Nathan F.
Nathan F.
30,773 Points

It looks like you've wrote the same test twice, but it's not certain this has anything to do with our other problems. Still, let's fix that. You have the "user should have a unique profile name" test twice, so delete one of these blocks:

test "a user should have a unique profile name" do
            user = User.new
            user.profile_name = users(:test).profile_name
            assert !user.save
            assert !user.errors[:profile_name].empty?
    end

Then run rake again and let me know what is output.

After deleting the extra blocks"user should have a unique profile name" i get lots of error again...... an again..... according the error i make some changes but still its not helping me.

$ bin/rake test

DL is deprecated, please use Fiddle

Run options: --seed 27600

Running:

EEEEEEEEEEEEEEEEEEEEEEEEEE

Finished in 0.157914s, 164.6466 runs/s, 0.0000 assertions/s.

1) Error: ProfilesControllerTest#test_only_show_the_correct_user's_statuses: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: ProfilesControllerTest#test_only_show_the_correct_user's_statuses: NoMethodError: undefined method `each' for nil:NilClass

2) Error: ProfilesControllerTest#test_should_get_show: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Error: ProfilesControllerTest#test_should_get_show: NoMethodError: undefined method `each' for nil:NilClass

hello.... When I removed the password and password_confirmation columns from the users fixture it passed the test with no errors. bt at the stage of testing profile name i found new error and it was from user_test.rb, when i make changes in regular expression "/^[a-zA-Z0-9_-]+$/".

My user.rb file:-

validates :profile_name, presence: true,
uniqueness: true, format: { with: /^[a-zA-Z0-9_-]+$/ , multiline: true, message: 'Must be formatted correctly.' }

Running test:-

$ ruby -Itest test/models/user_test.rb

DL is deprecated, please use Fiddle

Run options: --seed 50723

Running:

E.....

Finished in 0.303276s, 19.7840 runs/s, 36.2706 assertions/s.

1) Error: UserTest#test_a_user_can_have_a_correctly_formatted_profile_name: ArgumentError: wrong number of arguments (0 for 1..2) test/models/user_test.rb:41:in `block in <class:UserTest>'

6 runs, 11 assertions, 0 failures, 1 errors, 0 skips

Thanks for your help!

Nathan F.
Nathan F.
30,773 Points

Can you post the contents of your statuses.yml fixture file and the content of your statuses_controller_test.rb? The messages seem to be indicating syntax errors.

This is my statues.yml fixture file

Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one: context: MyText user: sarah

two: context: MyText user: sarah

three: context: MyText user: ruby

Nathan F.
Nathan F.
30,773 Points

First off, I believe that your fixtures should say content, not context. :) At least if you're following along with the Treehouse courses. Also, it may just be that the formatting got gobbled up when you pasted it into the window (use the markdown cheat sheet to see how to place a code window), but make sure the YAML is properly formatted, for example:

one:
  content: MyText
  user: sarah
two:
  content: MyText
  user: sarah
three:
  content: MyText
  user: ruby

it was my mistake....

Nathan F.
Nathan F.
30,773 Points

Did you happen to solve the issue? Or is it still there?

It seems as if this error is coming from a problem either within your fixtures file (it's saying the format is invalid), or possibly even your Statuses model/controller. It's telling us there's no method each for nil:NilClass. Most likely, the each method being referenced is the method where you cycle through each status. The problem is, rather than getting a list of statuses, the @statuses object is nil. This might be because you have an extra or missing end statement.

Let's try one last thing. Run:

rake test

or

bin/rake test

This will run all tests, which should (hopefully) catch any errors made elsewhere that could be affecting this test.

I done all these changes as u ask me to do, but still i am having same problem :(

Nathan F.
Nathan F.
30,773 Points

Can I also see the statuses_controller_test.rb file? Many of these errors are claiming there's no such method as each, which is surprising! Use three backticks (```), paste your code, then add another three backticks, so the formatting is preserved.