Displaying 20 results from an estimated 277 matches for "and_return".
2007 Oct 05
13
spec''ing view render partial collection, local variable not found
...game = mock_model(Game,
:name => ''The Battle for Blaze'',
:salt_grains => 5000000,
:people => 5000000,
:days => nil,
:created_at => "Mon Oct 01 00:02:44 -0400 2007",
:enabled => true)
game.should_receive(:name).and_return("The Battle for Blaze")
game.should_receive(:salt_grains).and_return(5000000)
game.should_receive(:people).and_return(5000000)
game.should_receive(:days).and_return(nil)
game.should_receive(:created_at).and_return("Mon Oct 01 00:02:44 -0400
2007")
game.should...
2007 Nov 20
3
How to test views with Nested Resources and Partials
...seconds
1 example, 1 failure
######## edit.haml_spec.rb ########
require File.dirname(__FILE__) + ''/../../spec_helper''
describe "/line_items/edit.haml" do
include LineItemsHelper
before do
@contract = mock_model(Contract)
@contract.stub!(:id).and_return("1")
@contract.stub!(:sdc_ref).and_return("MyString")
@contract.stub!(:description).and_return("MyString")
@contract.stub!(:sales_rep_id).and_return("1")
@contract.stub!(:office_id).and_return("1")
@contract.stub!(:account_id).an...
2007 Jan 05
4
How To Spec Controllers with Finders
Given this code (which renders rjs), I''m faced with the fixture-
driven way of spec-ing or mocking. How the heck to you mock this so
the code at line (2) and (4) work right? I''m still struggling with
mocks but it seems like this can be done. Forgive the naivety of this
question.
1. def change_quantity
2. @line_item = LineItem.find_by_id(params[:id])
3. unless
2008 Jan 28
2
Could this controller test be made simpler?
...and it is simpler, but it
seems that the relationship made between the two models is what starts
to make this more complex.
Thanks for the help.
Here is my current code:
===============
before :each do
@account = mock_model(Account)
#what is being tested here
@account.stub!(:save).and_return(false)
#User info
@user = mock_model(User)
users = mock("Userlist")
@account.stub!(:users).and_return(users)
users.stub!(:build).and_return(@user)
#since the form is repopulated the account mock must have stub for
all the attributes
@account.stub!(:subdomain)...
2007 Apr 19
5
Best practice thoughts: Model helpers, mocks
...like to avoid, one of my controller
test setups. It seems like each context, I end up pasting the
previous contexts setup, and then adding a few more lines, until it
ends up...
setup do
mem_types = Array.new
@membership_type1 = mock_model(MembershipType)
@membership_type1.stub!(:id).and_return(1)
@membership_type1.stub!(:name).and_return(''Name'')
@membership_type1.stub!(:price_cents).and_return(5000)
mem_types << @membership_type1
@membership_type2 = mock_model(MembershipType)
@membership_type2.stub!(:id).and_return(2)
@membership_type2.stub...
2007 Dec 29
15
Do you think it would look cleaner?
I was looking over some of my specs.
I was thinking that the following:
@game.should_receive(:name).and_return(''The Battle for Blaze'')
@game.should_receive(:people).and_return(5000000)
@game.should_receive(:activated).and_return(true)
Would it look cleaner if I could do this instead?
@game.should_recieve_and_return(
:name => ''The Battle for Blaze''
:people => 5...
2007 Apr 30
4
Mocking with Calculated Results
...case
(neglecting the optional parameters).
My question is: Is this calculation of the return value a bad thing,
and if so, how better to accomplish it?
Thanks
---------
module SettingsMock
def setup_mock
@countable = mock(''countable'')
@countable.stub!(:count).and_return(1)
@countable.stub!(:full_messages).and_return([''a message''])
@setting = mock_model Setting do |m|
m.stub!(:save).and_return(true)
m.stub!(:destroy)
m.stub!(:errors).and_return(@countable)
m.stub!(:setting_name).and_return(''first_name...
2007 Nov 21
7
describe AddressesController, "handling GET /addresses" do
...t; @addresses }
end
end
private
def get_company
@company = Company.find_by_id(params[:company_id])
end
My controller spec code for handling GET /addresses:
before do
@company = mock_model(Company)
@addresses = mock("addresses")
@company.stub!(:addresses).and_return(@addresses)
Company.stub!(:find).and_return(@company)
end
def do_get
get :index, :company_id => 1
end
it "should be successful" do
do_get
response.should be_success
end
.............
All of my tests (4) fail:
4) NoMethodError in ''AddressesControl...
2007 Jul 12
3
Agh, this is annoying. Why is this happening?
...th (1)
No matter what I do to try to stub that out it will still fail out and give
me that message.
Here is my spec
describe TasksController, "handling POST /tasks" do
before(:each) do
@task = mock_model(Task, :to_param => "1", :save => true)
Task.stub!(:new).and_return(@task)
@user = mock_model(User)
@user.stub!(:id).and_return(1)
@user.stub!(:login).and_return("moo")
User.stub!(:find).and_return(@user)
@params = {}
end
def do_post
@request.session[:user] = @user.id
post :create, :task => @params
end
it "sho...
2007 Jul 05
6
mocking methods in the controller.
Hi,
I''m very new to rspec so please be patient with me.
I''ve tried to take some of my tests out of the controller specs to check for
things that are rendered.
This has not worked so well, since my views have the controller method
current_user
in quite a few places.
Is there any way that I can define this so that my views will be executed?
Will this same thing occur for all
2007 Jun 02
7
I''m really bad at controllers, help please.
...@user.messages
end
end
So basically what I''m doing is listing all the messages for a user,
provided there is an id parameter.
describe MessagesController, " handling GET /messages for a user" do
before do
@message = mock_model(Message)
@message.stub!(:user_id).and_return(1)
@user = mock_model(User)
@user.stub!(:id).and_return(1)
User.stub!(:messages).and_return([@message])
User.stub!(:find).and_return([@user])
end
def do_get
get :index, :user_id => 1
end
it "should be successful" do
do_get
response.should be_succes...
2007 May 20
9
How to test for exceptions
...I''ve run into a slight problem. I tried looking through the documentation but I still don''t know what I missed.
Thanks in advance,
Blake
describe VenuesController, "on update" do
before(:each) do
@venue = mock("venue")
@venue.stub!(:new_record?).and_return(false)
@venue.stub!(:to_param).and_return(''1'')
controller.class.send(:define_method, :rescue_action) { |e| raise e }
end
it "should raise an error if record is invalid" do
stub_authorized_login
Venue.should_receive(:find).and_return(@venue)
@venu...
2007 Aug 17
11
[rspec] looking for a simple refresher
...6:
script/spec:4:
class TicketsController < ApplicationController
def new
Ticket.new
end
end
describe TicketsController, "handling POST /tickets" do
before do
@ticket = mock_model(Ticket, :to_param => ''1'', :save => true)
Ticket.stub!(:new).and_return(@ticket)
@params = {}
end
def do_post
post :create, :ticket => @params
end
it "should create a new ticket" do
@ticket.should_receive(:new).with(@params).and_return(@ticket)
do_post
end
end
Would someone provide with an explanation what I have to do to make...
2007 Oct 05
7
Easy AR association stubbing
...makes it pretty easy to
stub associations in rails. I''ve been using it for awhile and it seems
to cut down on a lot of setup code for the controller and model specs
that use associations.
#before
@person = mock_model(Person)
posts = mock(''post_proxy'')
posts.stub!(:build).and_return(mock_model(Post, :save => true))
@person.stub!(:posts).and_return(posts)
# now
@person = mock_model(Person)
@person.stub_association!(:posts, :find_by_title => mock_model(Post))
Just add this to the spec helper
module Spec
module Mocks
module Methods
def stub_association!(ass...
2007 Jul 24
6
Mocking Access Control
...ing how i should mock my user. The user has a habtm
relationship to a roles model (acl_system2 plugin), but I''m not sure
how to tell rspec about a model.
My code:
describe UsersController do
integrate_views
before(:each) do
@user = mock_model(User)
@user.stub!(:new_record?).and_return(false)
@user.stub!(:id).and_return(666)
@user.stub!(:email).and_return("john at doe.com")
@user.stub!(:password).and_return("dummypassword")
User.stub!(:new).and_return(@user)
end
it "should login as a tutor" do
@user.stub!(:type).and_return(&q...
2007 Sep 30
9
Problems with testing nested routes using mocking
....rb
describe VirtualHostsController, "handling GET /domains/1/virtual_hosts/1" do
before do
@domain = mock_model(Domain)
@virtual_hosts = mock("virtual_hosts")
@virtual_host = mock("virtual_host")
Domain.should_receive(:find).with("1").and_return(@domain)
@domain.should_receive(:virtual_hosts).and_return(@virtual_hosts)
@virtual_hosts.should_receive(:find).and_return(@virtual_host)
login_as :admin
end
def do_get
get :show, :id => "1", :domain_id => "1"
end
it "should render sho...
2009 Feb 20
6
How to mock an object defined in the before_filter function?
...function "find_comment" is called.
The way I do it below doesn''t work, since
"before_filter :find_comment" returns true/false and @comment
initialization is done inside it. Could you please give me a hint how
to do it?
One solution would be to use
Comment.stub!(:find).and_return(@comment) and do not use the stub
find_comment.
But how to do it in general, when there is no expression like "@var =
my_var.function" in the controller and variable @var is defined in
another place and controller just uses it.
my_var.stub!(:function).and_return(@var) doesn''t see...
2007 Nov 01
2
Can''t use #exactly when #and_return influences it.
...even if some elements are left. I was trying
to spec this last bit but ended up on a false positive.
Hope the example will be clear enough:
#spec_reader.rb
describe Reader do
it "should stop reading items when called with a limit" do
@stack = mock(Stack)
Stack.should_receive(:new).and_return(@stack)
@stack.should_receive(:read).exactly(3).times.and_return(2,4,1,5,3,1,false)
@reader = Reader.new
@reader.read_stack(3)
end
end
# reader.rb
class Reader
def initialize
@stack = Stack.new
end
def read_stack(limit = 0) #0 = read until false is returned
while val = @stack.read...
2007 Oct 26
3
Specing with Subdomains as Account Keys
...it to make it more clear with the
subdomain as account key question.)
# items_controller_spec.rb
describe ItemsController, "handling GET /items" do
fixtures :companies
before do
@request.host = "subdomain.test.host"
@item = mock(Item)
Item.stub!(:find).and_return([@item])
@current_company = mock(Company)
@current_company.stub!(:items).and_return([])
@current_company.stub!(:find_items).and_return([@item])
@current_company.stub!(:subdomain).and_return("subdomain")
end
# Passes
def do_get
get :index
end
# P...
2007 May 29
5
Trouble defining a stub method for a controller
Hello,
Not sure if I am doing something really wrong (let''s not say
stupid for now), but I haven''t been able to stub a controller method
like:
controller.stub!(:logged_in?).and_return(true)
Please help, this is driving me nuts ;-)
--
An?bal Rojas
http://www.rubycorner.com
http://www.hasmanydevelopers.com