Forum: RSpec Testing User Agent

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
948b5d3954b374679dd033de87c589aa?d=identicon&s=25 Suprie Leonhart (Guest)
on 2009-02-25 09:02
(Received via mailing list)
hi

i'm testing some lib i've made for detecting mobile user agent, I
confused
how to test the lib i've made.
the test code is

[code]

  describe "Check if mobile browser" do
    it "should know if it's from blackberry" do
      blackberry_ua = "BlackBerry8330/4.3.0 Profile/MIDP-2.0
Configuration/CLDC-1.1 VendorID/105"
      request.env["HTTP_USER_AGENT"] = blackberry_ua
      is_mobile?.should == true
    end
  end

[/code]

the error is, request variable always nil

the question : how i mock the request.env ?

--
--
Best Regards
Suprie
http://suprie.in.ruangkopi.com

"Mendapat upah karena menyenangkan orang lain yang tidak punya
persangkutan
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?"
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-25 10:44
(Received via mailing list)
On Wed, Feb 25, 2009 at 1:57 AM, Suprie Leonhart
<suprie.leonheart@gmail.com> wrote:
>       blackberry_ua = "BlackBerry8330/4.3.0 Profile/MIDP-2.0
> the question : how i mock the request.env ?
Since you're trying to test existing code, can you please post that
code?
948b5d3954b374679dd033de87c589aa?d=identicon&s=25 Suprie Leonhart (Guest)
on 2009-02-25 10:53
(Received via mailing list)
On Wed, Feb 25, 2009 at 4:19 PM, David Chelimsky
<dchelimsky@gmail.com>wrote:

> >
> >
> > the error is, request variable always nil
> >
> > the question : how i mock the request.env ?
>
> Since you're trying to test existing code, can you please post that code?
>

[code]
module Mobile
  def load_mobile_agent_file
    ma = File.read(RAILS_ROOT+"/config/mobile.yml")
    yaml = YAML.load(ma)
    return yaml
  end

  def mobile_user_agent?
    if request.env["HTTP_USER_AGENT"]
      user_agent  = request.env["HTTP_USER_AGENT"]
      mua         = load_mobile_agent_file
      mua["mobile_agent"].each do |key, value|
        unless(user_agent.match(Regexp.new(Regexp.escape(key))).nil?)
          return true
        end
      end
      return false
    end
  end
end
[/code]

--
--
Best Regards
Suprie
http://suprie.in.ruangkopi.com

"Mendapat upah karena menyenangkan orang lain yang tidak punya
persangkutan
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?"
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-25 16:47
(Received via mailing list)
On Wed, Feb 25, 2009 at 3:51 AM, Suprie Leonhart
<suprie.leonheart@gmail.com> wrote:
>> > confused
>> >       is_mobile?.should == true
>
> [code]
> module Mobile
>   def load_mobile_agent_file
>     ma = File.read(RAILS_ROOT+"/config/mobile.yml")
>     yaml = YAML.load(ma)
>     return yaml
>   end

Off topic, but this could be a one liner with no temporary variables,
and load_file will take care of opening and reading the file for you:

def load_mobile_agent_file
  YAML.load_file(RAILS_ROOT+"/config/mobile.yml")
end

>       return false
>     end
>   end
> end
> [/code]

Thanks - so I'm wondering where is_mobile? came from. Also, are you
trying to do this in a controller spec or a helper spec? Where is the
spec file located?
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2009-02-25 22:53
(Received via mailing list)
On 25/02/2009, at 4:51 AM, Suprie Leonhart wrote:
> >
> >
> module Mobile
>       mua["mobile_agent"].each do |key, value|
> --
> --
> Best Regards
> Suprie
> http://suprie.in.ruangkopi.com

Hi Suprie. AFAIK, RSpec populates the "request" object in your specs
after a controller action is called. Since your "should know if it's
from blackberry" example doesn't call a controller action, the
"request" object is nil.
-Nick
948b5d3954b374679dd033de87c589aa?d=identicon&s=25 Suprie Leonhart (Guest)
on 2009-02-26 03:55
(Received via mailing list)
On Wed, Feb 25, 2009 at 10:45 PM, David Chelimsky
<dchelimsky@gmail.com>wrote:

> >> >
> >> > Configuration/CLDC-1.1 VendorID/105"
> >>
>
> >       user_agent  = request.env["HTTP_USER_AGENT"]
> > [/code]
>
> Thanks - so I'm wondering where is_mobile? came from. Also, are you
> trying to do this in a controller spec or a helper spec? Where is the
> spec file located?
>
>
i've refactoring the is_mobile? to mobile_content?  i'm trying to spec a
lib, so i put it on spec/lib

--
--
Best Regards
Suprie
http://suprie.in.ruangkopi.com

"Mendapat upah karena menyenangkan orang lain yang tidak punya
persangkutan
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?"
948b5d3954b374679dd033de87c589aa?d=identicon&s=25 Suprie Leonhart (Guest)
on 2009-02-26 04:23
(Received via mailing list)
On Thu, Feb 26, 2009 at 4:39 AM, Nick Hoffman <nick@deadorange.com>
wrote:

>
> Hi Suprie. AFAIK, RSpec populates the "request" object in your specs after
> a controller action is called. Since your "should know if it's from
> blackberry" example doesn't call a controller action, the "request" object
> is nil.
> -Nick


so i should put the test on controller spec then ?

--
--
Best Regards
Suprie
http://suprie.in.ruangkopi.com

"Mendapat upah karena menyenangkan orang lain yang tidak punya
persangkutan
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?"
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2009-02-26 06:16
(Received via mailing list)
On 25/02/2009, at 9:49 PM, Suprie Leonhart wrote:
>
> --
> --
> Best Regards
> Suprie

Ultimately, you should be speccing your application's behaviour rather
than specific methods. If a controller action calls #mobile_content? ,
then write specs for that action, and make sure to cover the case(s)
when #mobile_content? is called.
-Nick
948b5d3954b374679dd033de87c589aa?d=identicon&s=25 Suprie Leonhart (Guest)
on 2009-02-26 07:15
(Received via mailing list)
On Thu, Feb 26, 2009 at 12:08 PM, Nick Hoffman <nick@deadorange.com>
wrote:

>>
> specs for that action, and make sure to cover the case(s) when
> #mobile_content? is called.
>

thanks a lot for the advice.



--
--
Best Regards
Suprie
http://suprie.in.ruangkopi.com

"Mendapat upah karena menyenangkan orang lain yang tidak punya
persangkutan
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?"
This topic is locked and can not be replied to.