wrong number of arguments ruby rspec - ruby

I'm trying to write unit tests for my code using rspec. I keep getting a "wrong number of arguments" error:
class MyClass
attr_accessor :env, :company,:size, :role, :number_of_hosts,:visability
def initialize(env, company, size, role, number_of_hosts, visability)
#env, #company, #size, #role, #number_of_hosts, #visability = env, company, size, role, number_of_hosts, visability
end
end
And here are my tests:
require_relative "../lib/MyClass.rb"
describe MyClass do
it "has an environment" do
MyClass.new("environment").env.should respond_to :env
end
it "has a company" do
MyClass.new("company").company.should respond_to :company
end
...
When I run rspec I get:
1) MyClass has an environment
Failure/Error: MyClass.new("environment").env.should respond_to :env
ArgumentError:
wrong number of arguments (1 for 6)
# ./lib/MyClass.rb:4:in `initialize'
# ./spec/MyClass_spec.rb:5:in `new'
# ./spec/MyClass_spec.rb:5:in `block (2 levels) in <top (required)>'
...
What am I missing?
EDIT
Sergio helped thanks...however
Sergio's answer worked...although I still have a further question:
Given the Class:
class Team
attr_accessor :name, :players
def initialize(name, players = [])
raise Exception unless players.is_a? Array
#name = name
raise Exception if #name && has_bad_name
#players = players
end
def has_bad_name
list_of_words = %w{crappy bad lousy}
list_of_words - #name.downcase.split(" ") != list_of_words
end
def favored?
#players.include? "George Clooney"
end
end
and spec...
require_relative "../lib/team.rb"
describe Team do
it "has a name" do
Team.new("random name").should respond_to :name
end
it "has a list of players" do
Team.new("random name").players.should be_kind_of Array
end
...
The tests pass without the same error...(This works fine: Team.new("random name"))
Any explanation?

Here is the error MyClass.new("environment"). As you have written def initialize(env, company, size, role, number_of_hosts, visability). So you should pass 6 parameters when you are calling MyClass#new method. But in practice you pass only one which is "environment". Thus you got the legitimate error - wrong number of arguments (1 for 6).

Related

Using Rspec, how can I test a method which calls a method from another class in ruby

My Transaction class has a method 'balance' which calls a method 'balance_after' from my Account class. I would like to make a test where I can check the 'balance_after' method is being called with the 'balance' method. I am new to both ruby and testing, so I appreciate any guidance! I am using simple-cov to get my coverage and i'd like to hit 100% if possible. I hope I have provided enough information. Thanks in advance!
Failures:
1) Transaction#balance checks balance
Failure/Error: #account.balance_after(self)
NoMethodError:
undefined method `balance_after' for :account:Symbol
# ./lib/transaction.rb:11:in `balance'
# ./spec/transaction_spec.rb:19:in `block (3 levels) in <top (required)>'
Finished in 0.04089 seconds (files took 0.87223 seconds to load)
10 examples, 1 failure
Failed examples:
rspec ./spec/transaction_spec.rb:18 # Transaction#balance checks balance
COVERAGE: 98.55% -- 68/69 lines in 4 files
+----------+--------------------+-------+--------+---------+
| coverage | file | lines | missed | missing |
+----------+--------------------+-------+--------+---------+
| 90.91% | lib/transaction.rb | 11 | 1 | 16 |
+----------+--------------------+-------+--------+---------+
My Transaction class
class Transaction
attr_reader :amount, :account, :timestamp
def initialize(amount, account)
#amount = amount
#account = account
#timestamp = Time.now.strftime("%d/%m/%Y")
end
def balance
#account.balance_after(self)
end
def formate
#amount > 0 ? (puts "#{#timestamp}|| #{#amount} || || #{balance}")
: (puts "#{#timestamp}|| || #{#amount.abs} || #{balance}")
end
end
My Account class
require_relative 'transaction'
class Account
attr_reader :balance, :transactions
HEADER = 'date || credit || debit || balance'
def initialize
#balance = 0
#transactions = []
end
def deposit(amount)
#balance += amount
#transactions << Transaction.new(amount, self)
end
def withdraw(amount)
#balance -= amount
#transactions << Transaction.new(-amount, self)
end
def balance_after(transaction)
index = #transactions.find_index(transaction)
#transactions[0..index].map(&:amount).sum
end
def print_statement
puts HEADER
#transactions.reverse.map(&:formate)
end
end
My Transaction spec
require 'transaction'
describe Transaction do
let(:transaction) { Transaction.new(:amount, :account) }
let(:timestamp) { Time.now.strftime('%d/%m/%Y') }
describe '#initilalize' do
it 'validates class' do
expect(transaction).to be_a Transaction
end
it 'stores dates' do
expect(transaction.timestamp).to eq timestamp
end
end
describe '#balance' do
it 'checks balance' do
expect(transaction.balance).to eq true
end
end
end
You have to call the method correctly. Transaction.new takes an amount, presumably a number, and some sort of Account object which has balance_after defined. You've given it two Symbols.
describe Transaction do
let(:account) { Account.new }
let(:amount) { 1.23 }
let(:transaction) { Transaction.new(amount, account) }
let(:timestamp) { Time.now.strftime('%d/%m/%Y') }
Then check that it returns what you expect. balance does not return true or false, it returns a balance. Specifically, it's just a pass through to account.balance_after(transaction), so test that.
describe '#balance' do
it 'checks balance' do
expect(transaction.balance).to eq account.balance_after(transaction)
end
end
This might seem circular, but this is just an integration test. account.balance_after would be unit tested as part of Account's tests. There's no need to repeat those tests here.

Instance variable showing up as `nil` in rspec

I have the following test code:
require_relative '../spec_helper'
describe Chess::King do
before do
#piece = Chess::King.new('king1',:black)
#board = Chess::Board.new
end
describe '#possible_moves' do
context "placing king at location 4,5" do
#board.grid[4][5] = #piece
subject {#piece.possible_moves(#board)}
it {is_expected.to eq([3,5],[3,6],[4,6],[5,6],[5,5])}
end
end
end
Why am I getting this error:
in block (3 levels) in <top (required)>': undefined methodgrid' for nil:NilClass (NoMethodError)
I am not sure about this line: #board.grid[4][5] = #piece.
My intention here is to assign the piece object to the grid instance variable of board (8x8 array).
Try using let instead:
require_relative '../spec_helper'
describe Chess::King do
let(:piece) { Chess::King.new('king1',:black) }
let(:board) { Chess::Board.new }
describe '#possible_moves' do
context "placing king at location 4,5" do
before(:each) { board.grid[4][5] = piece }
subject {piece.possible_moves(board)}
it {is_expected.to eq([3,5],[3,6],[4,6],[5,6],[5,5])}
end
end
end

Undefined method error while running rspec test

All,
I am getting the following undefined method errors below when running my rspec tests. When I have gotten this error before I had a method misspelled or my order of execution caused it. I checked both along with some other posts on StackOverflow, but nothing helped. Can anyone offer any guidance?
Rspec Failures:
FFFF
Failures:
1) Post vote methods #up_votes counts the number of votes with value = 1
Failure/Error: expect(#post.up_votes ).to eq(3)
NoMethodError:
undefined method `up_votes' for #<Post:0x007fe92f381a38>
# ./spec/models/post_spec.rb:14:in `block (4 levels) in <top (required)>'
2) Post vote methods #down_votes counts the number of votes with values = -1
Failure/Error: expect(#post.down_votes ).to eq(2)
NoMethodError:
undefined method `down_votes' for #<Post:0x007fe92a18c228>
# ./spec/models/post_spec.rb:20:in `block (4 levels) in <top (required)>'
3) Post vote methods #points returns the sum of all down and up votes
Failure/Error: expect(#post.points ).to eq(1) # 3 - 2
NoMethodError:
undefined method `points' for #<Post:0x007fe92986c620>
# ./spec/models/post_spec.rb:26:in `block (4 levels) in <top (required)>'
4) Vote validations value validation only allows -1 or 1 as values
Failure/Error: expect(#post.up_votes).to eq((-1) || eq(1))
NoMethodError:
undefined method `up_votes' for nil:NilClass
# ./spec/models/vote_spec.rb:5:in `block (4 levels) in <top (required)>'
Post_rspec
require 'rails_helper'
describe Post do
describe "vote methods" do
before do
#post = Post.create(title: 'Post title', body: 'Post bodies must be pretty long.')
3.times { #post.votes.create(value: 1) }
2.times { #post.votes.create(value: -1) }
end
describe '#up_votes' do
it "counts the number of votes with value = 1" do
expect(#post.up_votes ).to eq(3)
end
end
describe '#down_votes' do
it "counts the number of votes with values = -1" do
expect(#post.down_votes ).to eq(2)
end
end
describe '#points' do
it "returns the sum of all down and up votes" do
expect(#post.points ).to eq(1) # 3 - 2
end
end
end
end
vote_spec file
describe Vote do
describe "validations" do
describe "value validation" do
it "only allows -1 or 1 as values" do
expect(#post.up_votes).to eq((-1) || eq(1))
end
end
end
end
Post.rb
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
has_many :votes
has_one :summary
belongs_to :user #means the post table has the user table's primary key in it
belongs_to :topic
mount_uploader :avatar, AvatarUploader
default_scope {order('created_at DESC')}
validates :title, length: {minimum: 5}, presence: true
validates :body, length: {minimum: 20}, presence: true
def markdown_title
(render_as_markdown).render(self.title).html_safe
end
def markdown_body
(render_as_markdown).render(self.body).html_safe
end
private
def render_as_markdown
renderer = Redcarpet::Render::HTML.new
extensions = {fenced_code_blocks: true}
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
#return redcarpet
end
end
For the post_spec.rb errors, check the Post model (see file app/models/post.rb) has the following methods defined in it:
up_votes
down_votes
points
Consider including the code for post.rb in your original question too.
For the vote_spec.rb errors, there is no #post variable, it will be nil. This error message hints at this:
Failure/Error: expect(#post.up_votes).to eq((-1) || eq(1))
NoMethodError: undefined method `up_votes' for nil:NilClass

Writing a DSL like Thor gem in Ruby?

I'm trying to figure out how the Thor gem creates a DSL like this (first example from their README)
class App < Thor # [1]
map "-L" => :list # [2]
desc "install APP_NAME", "install one of the available apps" # [3]
method_options :force => :boolean, :alias => :string # [4]
def install(name)
user_alias = options[:alias]
if options.force?
# do something
end
# other code
end
desc "list [SEARCH]", "list all of the available apps, limited by SEARCH"
def list(search="")
# list everything
end
end
Specifically, how does it know which method to map the desc and method_options call to?
desc is pretty easy to implement, the trick is to use Module.method_added:
class DescMethods
def self.desc(m)
#last_message = m
end
def self.method_added(m)
puts "#{m} described as #{#last_message}"
end
end
any class that inherits from DescMethods will have a desc method like Thor. For each method a message will be printed with the method name and description. For example:
class Test < DescMethods
desc 'Hello world'
def test
end
end
when this class is defined the string "test described as Hello world" will be printed.

You have a nil object when you didn't expect it

I’m interested in the topic of Rails security and using Security on Rails. I'm on Implementing RBAC /page 142/ and i cannot get past the error in the subject.
Here is the code:
module RoleBasedControllerAuthorization
def self.included(base)
base.extend(AuthorizationClassMethods)
end
def authorization_filter
user = User.find(:first,
:conditions => ["id = ?", session[:user_id]])
action_name = request.parameters[:action].to_sym
action_roles = self.class.access_list[action_name]
if action_roles.nil?
logger.error "You must provide a roles declaration\
or add skip_before_filter :authorization_filter to\
the beginning of #{self}."
redirect_to :controller => 'root', :action => 'index'
return false
elsif action_roles.include? user.role.name.to_sym
return true
else
logger.info "#{user.user_name} (role: #{user.role.name}) attempted to access\
#{self.class}##{action_name} without the proper permissions."
flash[:notice] = "Not authorized!"
redirect_to :controller => 'root', :action => 'index'
return false
end
end
end
module AuthorizationClassMethods
def self.extended(base)
class << base
#access_list = {}
attr_reader :access_list
end
end
def roles(*roles)
#roles = roles
end
def method_added(method)
logger.debug "#{caller[0].inspect}"
logger.debug "#{method.inspect}"
#access_list[method] = #roles
end
end
And #access_list[method] = #roles line throwing following exception:
ActionController::RoutingError (You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]=):
app/security/role_based_controller_authorization.rb:66:in `method_added'
app/controllers/application_controller.rb:5:in `<class:ApplicationController>'
app/controllers/application_controller.rb:1:in `<top (required)>'
app/controllers/home_controller.rb:1:in `<top (required)>'
I'm using Rails 3.0.3 and Ruby 1.9.2. I'm storing session in database. In finally thank you for every advise.
It seems like you can't access #access_list in method_added. I would try
class << base
attr_accessor :access_list
#access_list = {}
end
Might not solve your particular problem, but otherwise you won't be able to call #access_list[method] = #roles if your access_list attribute is read-only.
I'm not sure if this is the problem, but this looks suspicious:
class << base
#access_list = {}
attr_reader :access_list
end
Shouldn't #access_list be a class variable ##access_list?
Your defining #access_list as a instance variable of the class but your accessing it in as a instance_variable of an instance of the class. The following should probably work:
module AuthorizationClassMethods
def access_list
#access_list ||={}
end
def method_added(method)
logger.debug "#{caller[0].inspect}"
logger.debug "#{method.inspect}"
access_list[method] = #roles
end
end
If you need Auhorization you might want to check out Cancan by Ryan Bates
https://github.com/ryanb/cancan

Resources