I tried several hours to mock. What I want is basically to send "http accept" let''s say "application/json" and want rails to send data back in json. Therefore, i need to mock that headers to do a test. I think I mocked that header already, but rails still sends me back in xml. Anyone could help me?
On Sat, Aug 15, 2009 at 5:35 AM, Chamnap<chamnapchhorn at gmail.com> wrote:> I tried several hours to mock. What I want is basically to send "http > accept" let''s say "application/json" and want rails to send data back > in json. Therefore, i need to mock that headers to do a test. I think > I mocked that header already, but rails still sends me back in xml. > Anyone could help me?I think you mean stub (mocking means setting an expectation that a message is received - sounds like you''re just trying to set a value for the controller to access). Assuming you''re using a rails version using rack (can''t remember when that was introduced - 2.1?), you can do this: it "should return json" do request.env[''ACCEPT''] = "application/json" get whatever_the_path_is ... end HTH, David
I did the same thing as you did, but it doesn''t work. The respond_to method still responses back in "application/xml". Any idea? On Aug 15, 8:49?pm, David Chelimsky <dchelim... at gmail.com> wrote:> On Sat, Aug 15, 2009 at 5:35 AM, Chamnap<chamnapchh... at gmail.com> wrote: > > I tried several hours to mock. What I want is basically to send "http > > accept" let''s say "application/json" and want rails to send data back > > in json. Therefore, i need to mock that headers to do a test. I think > > I mocked that header already, but rails still sends me back in xml. > > Anyone could help me? > > I think you mean stub (mocking means setting an expectation that a > message is received - sounds like you''re just trying to set a value > for the controller to access). > > Assuming you''re using a rails version using rack (can''t remember when > that was introduced - 2.1?), you can do this: > > it "should return json" do > ? request.env[''ACCEPT''] = "application/json" > ? get whatever_the_path_is > ? ... > end > > HTH, > David > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
On Sun, Aug 16, 2009 at 9:01 PM, Chamnap<chamnapchhorn at gmail.com> wrote:> I did the same thing as you did, but it doesn''t work. ?The respond_to > method still responses back in "application/xml". Any idea?It sounds like you''re pretty sure your code is correct and your tests are broken. Does the application work outside of the RSpec tests? Can you hit it with, say, ''curl'' with the json Accept header and determine if it does the right thing? If it does, then yes,your tests are broken. If the application _itself_ is returning XML, then the tests are working correctly: they''re reporting a failing test condition when your application is indeed failing. >8-> Fix the code in that case, not the test. -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org
On Aug 17, 9:19?am, Stephen Eley <sfe... at gmail.com> wrote:> It sounds like you''re pretty sure your code is correct and your tests > are broken. ?Does the application work outside of the RSpec tests? > Can you hit it with, say, ''curl'' with the json Accept header and > determine if it does the right thing?Well, I can say, I trust respond_to method in rails. It actually works outside of RSpec tests. Maybe Rails uses some other objects that needs to be stubbed or mocked. I tried to dig the respond_to method code, but I feel I am going further too much. Chamnap
On Aug 16, 2009, at 8:01 PM, Chamnap <chamnapchhorn at gmail.com> wrote:> I did the same thing as you did, but it doesn''t work. The respond_to > method still responses back in "application/xml". Any idea?Did you spell it ACCEPT ( all caps )?> > On Aug 15, 8:49 pm, David Chelimsky <dchelim... at gmail.com> wrote: >> On Sat, Aug 15, 2009 at 5:35 AM, Chamnap<chamnapchh... at gmail.com> >> wrote: >>> I tried several hours to mock. What I want is basically to send >>> "http >>> accept" let''s say "application/json" and want rails to send data >>> back >>> in json. Therefore, i need to mock that headers to do a test. I >>> think >>> I mocked that header already, but rails still sends me back in xml. >>> Anyone could help me? >> >> I think you mean stub (mocking means setting an expectation that a >> message is received - sounds like you''re just trying to set a value >> for the controller to access). >> >> Assuming you''re using a rails version using rack (can''t remember when >> that was introduced - 2.1?), you can do this: >> >> it "should return json" do >> request.env[''ACCEPT''] = "application/json" >> get whatever_the_path_is >> ... >> end >> >> HTH, >> David >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/ >> rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users