Anyone know why I'm getting this RSpec error? getting nil <-- - ruby-on-rails

def mock_category(stubs={})
#mock_category ||= mock_model(Category, stubs).as_null_object
end
describe "GET show" do
it "assigns the requested category as #category" do
Category.stub(:find).with("37") { mock_category }
get :show, :id => "37"
assigns(:category).should be(mock_category)
end
end
Which returns :
1) CategoriesController GET show assigns the requested category as #category
Failure/Error: assigns(:category).should be(mock_category)
expected Category_1002, got nil
I'm confused here, because this is a right out of the box controller that rspec set up. Why could this be failing?
My versions:
Rails 3.0.0.beta4
Ruby 1.8.7
RSpec 2.0.0.beta.10
Also tried this, same exact reproducible error with :
Rails 3.0.0
Ruby 1.8.7
RSpec 2.0.0.beta.20
The command I used to generate the specs were rails g scaffold Category
In my application.rb
config.generators do |g|
g.template_engine :haml
g.test_framework :rspec, :fixture => true, :views => false
end
UPDATE
This goes for any scaffolded controller by Rails 3, with RSpec2. Its guarenteed to fail. Anyone know how this is supposed to be written?

rspec-rails has a spec-suite it runs against itself that uses all the generators and runs all the generated specs and they all pass, so this should work. What versions of rspec, rails, and ruby are you using? What commands did you use to generate the Category model and CategoriesController?

The conflict comes from conflicts that occurred between Rspec Beta 10 and Rspec Beta 20, and Rails 3 Beta4, to Rails 3 release.
To solve this, I uninstalled haml, and installed haml-rails.
Then I deleted all the specs that were previously generated, and regenerated them.

Related

Upgrade to rspec 3 cause error when using should have(1).error_on

Since I updated my Gemfile and moved to rspec 3, in many tests, I'm getting a error for: way:
it "should reject attribute that are too short" do
short = "a" * 3
hash = #attr.merge(:details => short)
Deal.new(hash).should have(1).error_on(:details)
end
I'm getting this error:
Failure/Error: Deal.new(hash).should have(1).error_on(:details)
NoMethodError:
undefined method `have' for #<RSpec::ExampleGroups::Deal_2::TestsOnDealsModelsValidations>
I read I should now be using "expect" instead of should but here with have(1).error_on, how should I write it to comply with rspec 3?
I tried the following but it still does not work:
it "should reject attribute that are too short" do
short = "a" * 3
hash = #attr.merge(:details => short)
expect(Deal.new(hash).error_on(:details).size).to eq(1)
end
I have replaced the likes of
Deal.new(hash).should have(1).error_on(:details)
with
deal = Deal.new(hash)
expect(deal.valid?).to be_falsey
expect(deal.errors[:details].size).to eq(1)
The first expectation with valid? is necessary as it initializes the errors list.
have and other similar matchers have been moved out of rspec core and into another gem, rspec-collection-matchers.
I recommend following the upgrade path from rspec 2 -> 3 as detailed in the rspec docs: https://relishapp.com/rspec/docs/upgrade
Upgrade to rspec 2.99
Run your test suite
Fix deprecation warnings
Upgrade to rspec 3.
If you had done this you would have received a deprecation error with your code that would have also told you what to do to fix it.
The line to add to your Gemfile should be:
gem 'rspec-collection_matchers'

pry on rspec in cloud 9 ide

I am new to RoR. I have two problems here.
ISSUE 1
my rspec is
require 'spec_helper'
describe RefUserCategory do
describe "validations" do
it { should validate_presence_of(:user_category_description) }
end
end
my Ref_User_Category model is
class RefUserCategory < ActiveRecord::Base
validate :user_category_description , presence: true
has_many :Users
end
The error I got in Rspec is
Expected errors to include /can't be blank/ when user_category_description is set to nil, got no errors
So I decided to to use Pry to check whats happening
ISSUE 2
Im my application folder I do
pry -r ./config/environment
[2] pry(main)> Ref_User_Category.new
LoadError: Unable to autoload constant Ref_User_Category, expected /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/736003/east/app/models/ref_user_category.rb to define it
from /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/lib/ruby/gems/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:464:in `load_missing_constant'
I have user model too I
pry(main)> User.new
gives error
ActiveRecord::StatementInvalid: Could not find table 'users'
/app-root/data/lib/ruby/gems/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:512:in `table_structure'
Regarding issue 1, you have a typo: change validate to validates.
Regarding issue 2:
You generally start the console from the root of your project like this:
$ rails c
You are doing Ref_User_Category.new when you should do RefUserCategory.new.
About User.new, it looks like you haven't run your database migrations:
$ rake db:migrate
$ rake db:test:prepare

Generate all RSpec spec files from existing controllers, models & views in a Rails app

Does anyone know of a rake task or RSpec call that will generate a bunch of empty files relative to the existing controllers, models, helper files and views that already exist within your application?
You can generate an empty scaffold set of rspec tests against an existing controller using something like this:
rails generate rspec:scaffold recipe
You can improve on this by passing the attributes of the model you want to generate against, like this:
rails generate rspec:scaffold recipe title: string slug: string description: text
You'll still need to do some manual editing, but this should get you most of the way there.
The best solution for this is to add hooks in place within environment.rb to create the spec.rb files within the rails application each time a model or controller is created.
Here's the code for that (using RSpec and FactoryGirl):
module RailsApp
class Application < Rails::Application
config.generators do |g|
g.test_framework :rspec, :fixture_replacement => :factory_girl, :views => true, :helper => false
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.stylesheets false
g.javascripts false
g.helper false
end
end
end
This should work:
Install the rspec-rails gem by adding it to your development and test groups in your gemfile gem 'rspec-rails'
Run the rspec generator from inside your app rails generate rspec:install
Read over this doc quickly to see how it integrates with your rails app RSpec-rails doc

Setting up Shoulda under Test/Unit in Rails 3 (3.0.3)

I have posted this in other places but no response. Trying to get Shoulda working inside Test/Unit in Rails 3.0.3 (1.9.2). When I try to run the test (copied below), I get this error:
test/unit/practice_member_test.rb:4:in <class:PracticeMemberTest>': undefined methodcontext' for PracticeMemberTest:Class (NoMethodError)
Note that I have another Rails 3 project with Rspec including Shoulda also and it works fine via Rspec. In the failing project I tried placing "require 'shoulda'" in test helper to no avail, but when I run the debugger and type Shoulda, the object is found, so the library is being loaded.
Here is my test:
require 'test_helper'
class PracticeMemberTest < Test::Unit::TestCase
context "practice member" do
should "get global practice member count not including Demo Practice" do
assert_equal PracticeMember.practice_members_global_count, 0
practice = Factory.create(:practice_one)
practice_member = Factory.create(:practice_member)
practice_member.practice_id = practice.id
practice_member.save
practice_member = Factory.create(:practice_member)
practice_member.practice_id = practice.id
practice_member.save
assert_equal PracticeMember.practice_members_global_count, 2
end
end
end
Must be something I am overlooking as I have not seen anyone with this same issue.
Did you try adding the following to your config/environment.rb file:
Rails::Initializer.run do |config|
config.gem "shoulda", :lib => "shoulda"
end
Then
$ rake gems:install
$ rake gems:unpack
As suggested in the documentation?

Rails Unit Test Issue with rake

I am working on writing tests for a rails 2.3.4 application and I am running into the following error when I try to run the tests
1) Failure:
default_test(ReportTest) [rake (0.8.7) lib/rake/rake_test_loader.rb:5]:
No tests were specified.
This is what the only test file looks like:
require File.dirname(__FILE__) + '/../test_helper'
class UserTest < ActiveSupport::TestCase
include Authlogic::TestCase
setup :activate_authlogic
fixtures :users
def setup
#user = users(:one)
end
def test_user_is_valid
assert #user.valid?
end
end
One issue I could forsee being a problem is that I have multiple versions of rails installed and rake as well
rails (3.0.0, 2.3.8, 2.3.5, 2.3.4, 1.2.6)
rake (0.8.7, 0.8.3)
Anyone know what's going on?
Somehow the search for tests that rake test does has turned up a file named _test.rb which doesn't have a test in it. It will look through all the files under the test/units /functionals and /integration directories to try and locate tests.
I also once had the strange behaviour that a rails application in a sub directory of another rails application was finding more tests than it should have done as it was picking up tests from the 'parent' application. Might be worth checking that too.

Resources