Frank
Frank

Reputation: 189

Testing my models method in rails

I have to test the creation of my account. This is my Account.create() in controllers method.

Account.create( account: { name: "#{params[:account][:name]}", 
    description: "#{params[:description]}" }, user: { email: current_user.name })

I have to test my model method Account.create().

post :create, '/account'

Below is my spec class.

require 'spec_helper'

describe Account do
  before(:all) do

  end

  it "can create an account" do
    FactoryGirl.create :account, name: "something", description: "something"
    # Account.create()
  end

end

I am not sure on how to procced.

Upvotes: 0

Views: 41

Answers (2)

Andrii Furmanets
Andrii Furmanets

Reputation: 1161

Please, take a look at this article, it explains how to test Models. I your case it should be like this:

it "can create an account" do
  FactoryGirl.create(:account, name: "something", description: "something").should be_valid

end

Upvotes: 1

Jasper
Jasper

Reputation: 2491

FactoryGirl is a tool to use when the methods you want to test need a complicated predefined state. When account creation has no dependencies you could simply do something like:

it "can create an account" do
  acc = Account.create(name: 'something', description:'something')

  acc.name.should == 'something'
  acc.description.should == 'something'
end

Upvotes: 1

Related Questions