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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:10:12+00:00 2026-06-07T15:10:12+00:00

My Rails models: task has_many positions. Scenario: When I create a new position, it

  • 0

My Rails models: task has_many positions.

Scenario: When I create a new position, it should create itself a task. I’d like to test that, and I’m doing it like this:

context "creating a new position" do
  let(:position) { create :position, name: 'Read some books', :task => nil }

  it "should create a simple task" do
    Task.find_by_name('Read some books').should be_nil # First should
    position # Execute let() block (FactoryGirl is lazy evaluating)
    Task.find_by_name('Read some books').should_not be_nil # Second (more relevant) should
  end
end

So how should I improve my test? The first “should” simply makes sure that there isn’t already a Task, so we can be sure that creating the Position creates the Task. But this violates the “only one should per it block” principle. So what about this?

context "creating a new position" do
  let(:position) do
    position = create :position, name: 'Read some books', :task => nil
    Task.delete_all
    position
  end

  it "should create a simple task" do
    position # Execute let() block (FactoryGirl is lazy evaluating)
    Task.find_by_name('Read some books').should_not be_nil
  end
end

Or should I simply count on the fact that there shouldn’t be such a task anyways (because a clean test db wouldn’t have one)? Thanks for your opinions.

Update (Solution)

After some research I found the change matcher of RSpec:

let(:position) { create :position, name: 'Read some books', :task => nil }

it "should create a simple task" do
  # Thanks to FactoryGirl's lazy evaluation of let(), the position doesn't yet exist in the first place, and then after calling position in the expect{} block, it is created.
  expect { position }.to change{ Task.count(conditions: { name: 'Read some books' }) }.by(1)
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-07T15:10:13+00:00Added an answer on June 7, 2026 at 3:10 pm

    RSpec 2.11 allows you to pass a block to change, and it expects the return value of the block to be the thing that changes. I would expect this to work for you:

    expect { position }.to change { Task.where(:name => 'Read some books').count }.from(0).to(1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What should I do to load rails models for rake task which is part
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
In models and controllers, we often use Rails macros like before_validation , skip_before_filter on
In my Rails 2.3.2 app I have 2 models: class Post has_many :approved_comments, :class_name
Rails does model loading on demand. For a rake task that I'm writing, I
I have two models Project and 'Task` where project has_many tasks and task belongs
Models class Project has_many :tasks class Task belongs_to :project Task has the following attributes
My setup: Rails 3.0.9, Ruby 1.9.2 Here are my models class Project has_many :tasks
I'm new to rails and like many I seem to be having challenges figuring
We have two Rails models: Person and Administrator . We're disallowing removal of Administrator

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.