Tuesday, 15 January 2013

ruby on rails 3 - Testing creating status is failing -



ruby on rails 3 - Testing creating status is failing -

my test not creating guideline , cannot work out why.

the test in guidelines_controller_test.rb is

test "should create guideline when logged in" sign_in users(:tester) assert_difference('guideline.count') post :create, guideline: { content: @guideline.content, hospital: @guideline.hospital, title: @guideline.title } end

my create action in guidelines_controller.rb is

def create @guideline = guideline.new(params[:guideline]) respond_to |format| if @guideline.save format.html { redirect_to @guideline, notice: 'guideline created.' } format.json { render json: @guideline, status: :created, location: @guideline } else format.html { render action: "new" } format.json { render json: @guideline.errors, status: :unprocessable_entity } end end

end

when seek run test fails

1) failure: test_should_create_guideline_when_logged_in(guidelinescontrollertest) [test/functional/guidelines_controller_test.rb:36]: "guideline.count" didn't alter 1. <4> expected <3>.

and test.log shows (have tried re-create relevant bit)

processing guidelinescontroller#create html parameters: {"guideline"=>{"content"=>"www.test.com", "hospital"=>"test hospital", "title"=>"test title"}} user load (0.3ms) select "users".* "users" "users"."id" = 781720531 limit 1 (0.1ms) savepoint active_record_1 guideline exists (0.3ms) select 1 1 "guidelines" (lower("guidelines"."title") = lower('test title') , "guidelines"."hospital" = 'test hospital') limit 1 (0.1ms) rollback savepoint active_record_1 rendered guidelines/_form.html.erb (256.5ms) completed 200 ok in 313ms (views: 279.2ms | activerecord: 0.8ms) (0.2ms) select count(*) "guidelines" (0.1ms) rollback transaction

can help?

it looks you're trying create guideline title/hospital combination exists. got right chunk of log - line:

guideline exists (0.3ms) select 1 1 "guidelines" [...]

is rails making sure no duplicate guideline exists (you presumably have "unique" validation in model). finds match, cancels save transaction:

(0.1ms) rollback savepoint active_record_1

change title and/or hospital you're trying insert, , should go through fine.

edit:

based on our conversation in comments, think problem follows: you're initializing @guideline @guideline = guidelines(:one), loads guideline named "one" defined in fixtures file.

however, when start running tests in rails, loads of fixtures test db automatically. when seek create new guideline attributes @guideline, you're guaranteed duplicate!

the simplest way around define new attributes in-line in test code:

post :create, guideline: { content: "this new content!", hospital: "some random hospital", title: "some random title" }

hope helps!

ruby-on-rails-3

No comments:

Post a Comment