Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8409013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:47:26+00:00 2026-06-09T23:47:26+00:00

Here is my controller test: test should get create do sign_in(FactoryGirl.create(:user, admin: true)) assert_difference(‘Event.count’)

  • 0

Here is my controller test:

 test "should get create" do
    sign_in(FactoryGirl.create(:user, admin: true))
    assert_difference('Event.count') do
      post :create, FactoryGirl.build(:event)
    end
    assert_not_nil assigns(:event)
    assert_response :success
  end

and when I add the simplest validation to events.rb

class Event < ActiveRecord::Base
  attr_accessible :city, :date, :name, :state, :street
  has_many :periods

  validates :name, presence: true
end

I get:

1) Failure:
test_should_get_create(EventsControllerTest) [/Users/noahc/Dropbox/mavens/test/functional/events_controller_test.rb:37]:
"Event.count" didn't change by 1.
<2> expected but was
<1>.

But, then I look at events_factory.rb

 factory :event do
    name 'First Event'
    street '123 street'
    city 'Chicago'
    state 'IL'
    date Date.today
  end

And there doesn’t seem to be an issue with name being required.

update:

When I make my test:

test "should get create" do
    sign_in(FactoryGirl.create(:user, admin: true))

    assert_not_nil assigns(:event)
    assert_response :success
  end

I get:

 1) Failure:
test_should_get_create(EventsControllerTest) [/Users/noahc/Dropbox/mavens/test/functional/events_controller_test.rb:38]:
<nil> expected to not be nil.

When I remove that line, and leave in the assert_response :success it passes.

update 2:

def create
    @event = Event.new(params[:event])
    @event.save
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:47:28+00:00Added an answer on June 9, 2026 at 11:47 pm

    What about changing this:

    post :create, FactoryGirl.build(:event)
    

    to:

    post :create, event: FactoryGirl.attributes_for(:event)
    

    Explanation:

    post expects a hash with the attributes of the record you’re creating. FactoryGirl.build(:event) creates a new unsaved instance of the model (event), which is not what you want. Since you had no validations on your model, this was somehow getting by and being ignored, so in fact the factory was having no influence on the newly-created event (which I assume was being created with blank attributes).

    attributes_for, in contrast, returns the attributes of the factory as a hash, so:

    attributes_for(:event) #=> { name: 'First Event', street: '123 street', ... }
    

    which is exactly what you want. When you pass this to post, it assigns the attributes from the hash to params, which are then used to create a new event in the line: Event.new(params[:event]).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my current code in the controller. @rating = (@approvals.count / @user.pages.count) +
Here is my test in my controller: describe GET index do it assigns all
Here is my JsonResult in my controller: // // GET: /Home/GetTags/ public JsonResult GetTags()
I have the following routes in my app: GET /admin/comments(.:format) {:controller=>admin/comments, :action=>index} admin_comments POST
Here is the create in rfq controller: def create if has_create_right? @rfq = Rfq.new(params[:rfq],
My rspec title test is failing with: 1) HomeController Get 'index should have the
This following Controller test is failing, and I can't figure out why: describe GET
I searched and searched, but nothing solved my problem. Here's my controller: def show
here is my code in Controller var q = context.post; return View(q); in view
Here I've a controller with 4 actions and i wanna apply application layout (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.