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 7858529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:29:32+00:00 2026-06-02T21:29:32+00:00

I have User , Account , and Role models. A User can exist by

  • 0

I have User, Account, and Role models. A User can exist by itself. An Account must be tied to a User through a Role. An Account must have at least one Role record of type “Owner”. I’m not sure how to test this in RSpec and FactoryGirl.

# user_id, account_id, role
Role < ActiveRecord::Base
  belongs_to :user
  belongs_to :account
end

User < ActiveRecord::Base
  has_many :roles
  has_many :accounts, through: roles
  accepts_nested_properties_for :accounts
end

Account < ActiveRecord::Base
  has_many :roles
  has_many :users, through: roles
end

If a user is not signed in, the Accounts.new will display the User and Account forms. If a user is signed in, only the Account form will be displayed. The trouble is, I’m unsure what Role and Account will look like in RSpec when trying to test the associations.

This test fails (the array comes back empty):

describe Account do
  let(:user) { FactoryGirl.create(:user) }
  before { @account = user.accounts.build(title: "ACME Corporation", subdomain: "acme") }
  subject { @account }
  it { should respond_to(:users) }
  its(:users) { should include(user) }

Then there is the added complication of testing when users are signed in, and when they are not. Any reference sample code I can see for a similar use case? I’m also not sure what to test for in roles_spec, and what belongs to accounts_spec/user_spec.

Factories:

FactoryGirl.define do
  factory :user do
    name                  "Mickey Mouse"
    email                 "mickey@disney.com"
    password              "m1ckey"
    password_confirmation "m1ckey"
  end
  factory :account do
    title     "ACME Corporation"
    subdomain "acme"
  end
  factory :role do
    user
    account
  end
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-02T21:29:33+00:00Added an answer on June 2, 2026 at 9:29 pm

    You test objects that depend on other objects by using a mocking framework to “mock” the other objects. There are a number of mock frameworks that allow you to do this.

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

Sidebar

Related Questions

I have User, Account, and Role models. Role stores the relationship type between Account
I have a User and Account models with has_one association and nested attributes. My
when calling a SSIS package (C# app) using LoadFromSqlServer, does the user account have
I have a user Sql Authentication account that has remote access to my database
I have a program that creates a Windows user account using the NetUserAdd() API
I have implemented endless scrolling to my website user account area and before more
How do I change a local user account password remotely using VB.NET/C#? I have
I have a User model and an Account model. The user has many accounts
I have a User and an Admin role in my project. I created my
Does anyone have any schema's, data models, or suggestions for creating a 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.