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

  • Home
  • SEARCH
  • 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 8090309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:40:30+00:00 2026-06-05T19:40:30+00:00

I have the following spec: it saves a default ‘active’ status if not specified

  • 0

I have the following spec:

  it "saves a default 'active' status if not specified" do
    if User.count <= 0
      user = User.new(FactoryGirl.attributes_for(:user))
      user.save
    end

    project = FactoryGirl.create(:project, :status => nil)
    project.status.should eq("active")
  end

With this FactoryGirl object:

  factory :project do
    title "A short title"
    description "A short description"
    parent_id 1
    pm_id 1
    status "active"
  end

The project’s model validates the presence of an association… so, before testing, I would like to save a user inside test database. But the code above doesn’t save anything in the DB.

Instead, If I add a before(:all) block:

before(:all) do
@user = User.new(FactoryGirl.attributes_for(:user))
@user.save
end

a new user gets saved into the database. Why?

  • 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-05T19:40:31+00:00Added an answer on June 5, 2026 at 7:40 pm

    If, in your model, the Project belongs_to the User then you can have factory girl simply create a user as it creates a project. You would just update your Project factory to

    factory :project do
        title "A short title"
        description "A short description"
        parent_id 1
        pm_id 1
        status "active"
        user
    end
    

    I think this would have the same effect as the code you have above.

    Secondly in the code above you don’t appear to be associating the project with the user at any point. (Assuming of course that neither pm_id or parent_id refer to the user, which case can I recommend that you don’t make assumptions about the ids and use the method above instead 🙂

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

Sidebar

Related Questions

I have following spec to test controller method: context #create do it should redirect
In a branch spec, I have the following view: //depot/dev/t/a/g/... //depot/dev/t/r/g/... -//depot/dev/t/a/g/p/o*/... //depot/dev/t/r/g/p/... Perforce
I have tree with the following node spec: java.util.TreeMap<Long id, java.util.TreeMap children> When I
I have the following XML example: <?xml version=1.0 encoding=UTF-8?> <OpenSearchDescription xmlns=http://a9.com/-/spec/opensearch/1.1/ xmlns:custom=http://example.com/opensearchextensions/1.0/> <ShortName>Test</ShortName> <Description>Testing....</Description>
I have the following spec (using Machine.Specifications or mSpec): public class when_a_user_logs_in_successfully { static
In the following code I have to reuse the Active Pattern result three times
I´m testing my rails app using rspec and remarkable. I have the following spec
I have two models. class User has_one :spec, :dependent => :destroy # returns true
I have the following method to create a new Connection object. It will open
I have following Rspec test: describe Productlimit do before(:each) do @productlimit = Factory.create(:productlimit, :user

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.